<!doctype html>
<html>
<head>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-114672259-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', 'UA-114672259-1');
  gtag('set', {'user_id': 'USER_ID'});
</script>
<meta charset='UTF-8'><meta name='viewport' content='width=device-width initial-scale=1'>
<title>R_basic_learning</title><link href='https://fonts.loli.net/css?family=Ubuntu:400,700,400italic,700italic' rel='stylesheet' type='text/css' /><link href='https://fonts.loli.net/css?family=Raleway:600,400&subset=latin,latin-ext' rel='stylesheet' type='text/css' /><style type='text/css'>html {overflow-x: initial !important;}:root { --bg-color: #ffffff; --text-color: #333333; --select-text-bg-color: #B5D6FC; --select-text-font-color: auto; --monospace: "Lucida Console",Consolas,"Courier",monospace; }
html { font-size: 14px; background-color: var(--bg-color); color: var(--text-color); font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; }
body { margin: 0px; padding: 0px; height: auto; bottom: 0px; top: 0px; left: 0px; right: 0px; font-size: 1rem; line-height: 1.42857143; overflow-x: hidden; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
iframe { margin: auto; }
a.url { word-break: break-all; }
a:active, a:hover { outline: 0px; }
.in-text-selection, ::selection { text-shadow: none; background: var(--select-text-bg-color); color: var(--select-text-font-color); }
#write { margin: 0px auto; height: auto; width: inherit; word-break: normal; word-wrap: break-word; position: relative; white-space: normal; overflow-x: visible; padding-top: 40px; }
#write.first-line-indent p { text-indent: 2em; }
#write.first-line-indent li p, #write.first-line-indent p * { text-indent: 0px; }
#write.first-line-indent li { margin-left: 2em; }
.for-image #write { padding-left: 8px; padding-right: 8px; }
body.typora-export { padding-left: 30px; padding-right: 30px; }
.typora-export .footnote-line, .typora-export li, .typora-export p { white-space: pre-wrap; }
@media screen and (max-width: 500px) { 
  body.typora-export { padding-left: 0px; padding-right: 0px; }
  .CodeMirror-sizer { margin-left: 0px !important; }
  .CodeMirror-gutters { display: none !important; }
}
#write li > figure:first-child { margin-top: -20px; }
#write ol, #write ul { position: relative; }
img { max-width: 100%; vertical-align: middle; }
button, input, select, textarea { color: inherit; font-family: inherit; font-size: inherit; font-style: inherit; font-variant-caps: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; }
input[type="checkbox"], input[type="radio"] { line-height: normal; padding: 0px; }
*, ::after, ::before { box-sizing: border-box; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p, #write pre { width: inherit; }
#write h1, #write h2, #write h3, #write h4, #write h5, #write h6, #write p { position: relative; }
h1, h2, h3, h4, h5, h6 { break-after: avoid-page; break-inside: avoid; orphans: 2; }
p { orphans: 4; }
h1 { font-size: 2rem; }
h2 { font-size: 1.8rem; }
h3 { font-size: 1.6rem; }
h4 { font-size: 1.4rem; }
h5 { font-size: 1.2rem; }
h6 { font-size: 1rem; }
.md-math-block, .md-rawblock, h1, h2, h3, h4, h5, h6, p { margin-top: 1rem; margin-bottom: 1rem; }
.hidden { display: none; }
.md-blockmeta { color: rgb(204, 204, 204); font-weight: 700; font-style: italic; }
a { cursor: pointer; }
sup.md-footnote { padding: 2px 4px; background-color: rgba(238, 238, 238, 0.701961); color: rgb(85, 85, 85); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; cursor: pointer; }
sup.md-footnote a, sup.md-footnote a:hover { color: inherit; text-transform: inherit; text-decoration: inherit; }
#write input[type="checkbox"] { cursor: pointer; width: inherit; height: inherit; }
figure { overflow-x: auto; margin: 1.2em 0px; max-width: calc(100% + 16px); padding: 0px; }
figure > table { margin: 0px !important; }
tr { break-inside: avoid; break-after: auto; }
thead { display: table-header-group; }
table { border-collapse: collapse; border-spacing: 0px; width: 100%; overflow: auto; break-inside: auto; text-align: left; }
table.md-table td { min-width: 32px; }
.CodeMirror-gutters { border-right-width: 0px; background-color: inherit; }
.CodeMirror { text-align: left; }
.CodeMirror-placeholder { opacity: 0.3; }
.CodeMirror pre { padding: 0px 4px; }
.CodeMirror-lines { padding: 0px; }
div.hr:focus { cursor: none; }
#write pre { white-space: pre-wrap; }
#write.fences-no-line-wrapping pre { white-space: pre; }
#write pre.ty-contain-cm { white-space: normal; }
.CodeMirror-gutters { margin-right: 4px; }
.md-fences { font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; overflow: visible; white-space: pre; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; position: relative !important; background-position: inherit inherit; background-repeat: inherit inherit; }
.md-diagram-panel { width: 100%; margin-top: 10px; text-align: center; padding-top: 0px; padding-bottom: 8px; overflow-x: auto; }
#write .md-fences.mock-cm { white-space: pre-wrap; }
.md-fences.md-fences-with-lineno { padding-left: 0px; }
#write.fences-no-line-wrapping .md-fences.mock-cm { white-space: pre; overflow-x: auto; }
.md-fences.mock-cm.md-fences-with-lineno { padding-left: 8px; }
.CodeMirror-line, twitterwidget { break-inside: avoid; }
.footnotes { opacity: 0.8; font-size: 0.9rem; margin-top: 1em; margin-bottom: 1em; }
.footnotes + .footnotes { margin-top: 0px; }
.md-reset { margin: 0px; padding: 0px; border: 0px; outline: 0px; vertical-align: top; text-decoration: none; text-shadow: none; float: none; position: static; width: auto; height: auto; white-space: nowrap; cursor: inherit; line-height: normal; font-weight: 400; text-align: left; box-sizing: content-box; direction: ltr; background-position: 0px 0px; background-repeat: initial initial; }
li div { padding-top: 0px; }
blockquote { margin: 1rem 0px; }
li .mathjax-block, li p { margin: 0.5rem 0px; }
li { margin: 0px; position: relative; }
blockquote > :last-child { margin-bottom: 0px; }
blockquote > :first-child, li > :first-child { margin-top: 0px; }
.footnotes-area { color: rgb(136, 136, 136); margin-top: 0.714rem; padding-bottom: 0.143rem; white-space: normal; }
#write .footnote-line { white-space: pre-wrap; }
@media print { 
  body, html { border: 1px solid transparent; height: 99%; break-after: avoid-page; break-before: avoid-page; }
  #write { margin-top: 0px; padding-top: 0px; border-color: transparent !important; }
  .typora-export * { -webkit-print-color-adjust: exact; }
  html.blink-to-pdf { font-size: 13px; }
  .typora-export #write { padding-left: 32px; padding-right: 32px; padding-bottom: 0px; break-after: avoid-page; }
  .typora-export #write::after { height: 0px; }
  @page { margin: 20mm 0px; }
}
.footnote-line { margin-top: 0.714em; font-size: 0.7em; }
a img, img a { cursor: pointer; }
pre.md-meta-block { font-size: 0.8rem; min-height: 0.8rem; white-space: pre-wrap; background-color: rgb(204, 204, 204); display: block; overflow-x: hidden; background-position: initial initial; background-repeat: initial initial; }
p > .md-image:only-child:not(.md-img-error) img, p > img:only-child { display: block; margin: auto; }
p > .md-image:only-child { display: inline-block; width: 100%; }
#write .MathJax_Display { margin: 0.8em 0px 0px; }
.md-math-block { width: 100%; }
.md-math-block:not(:empty)::after { display: none; }
[contenteditable="true"]:active, [contenteditable="true"]:focus { outline: 0px; box-shadow: none; }
.md-task-list-item { position: relative; list-style-type: none; }
.task-list-item.md-task-list-item { padding-left: 0px; }
.md-task-list-item > input { position: absolute; top: 0px; left: 0px; margin-left: -1.2em; margin-top: calc(1em - 10px); }
.math { font-size: 1rem; }
.md-toc { min-height: 3.58rem; position: relative; font-size: 0.9rem; border-top-left-radius: 10px; border-top-right-radius: 10px; border-bottom-right-radius: 10px; border-bottom-left-radius: 10px; }
.md-toc-content { position: relative; margin-left: 0px; }
.md-toc-content::after, .md-toc::after { display: none; }
.md-toc-item { display: block; color: rgb(65, 131, 196); }
.md-toc-item a { text-decoration: none; }
.md-toc-inner:hover { text-decoration: underline; }
.md-toc-inner { display: inline-block; cursor: pointer; }
.md-toc-h1 .md-toc-inner { margin-left: 0px; font-weight: 700; }
.md-toc-h2 .md-toc-inner { margin-left: 2em; }
.md-toc-h3 .md-toc-inner { margin-left: 4em; }
.md-toc-h4 .md-toc-inner { margin-left: 6em; }
.md-toc-h5 .md-toc-inner { margin-left: 8em; }
.md-toc-h6 .md-toc-inner { margin-left: 10em; }
@media screen and (max-width: 48em) { 
  .md-toc-h3 .md-toc-inner { margin-left: 3.5em; }
  .md-toc-h4 .md-toc-inner { margin-left: 5em; }
  .md-toc-h5 .md-toc-inner { margin-left: 6.5em; }
  .md-toc-h6 .md-toc-inner { margin-left: 8em; }
}
a.md-toc-inner { font-size: inherit; font-style: inherit; font-weight: inherit; line-height: inherit; }
.footnote-line a:not(.reversefootnote) { color: inherit; }
.md-attr { display: none; }
.md-fn-count::after { content: "."; }
code, pre, samp, tt { font-family: var(--monospace); }
kbd { margin: 0px 0.1em; padding: 0.1em 0.6em; font-size: 0.8em; color: rgb(36, 39, 41); background-color: rgb(255, 255, 255); border: 1px solid rgb(173, 179, 185); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; box-shadow: rgba(12, 13, 14, 0.2) 0px 1px 0px, rgb(255, 255, 255) 0px 0px 0px 2px inset; white-space: nowrap; vertical-align: middle; background-position: initial initial; background-repeat: initial initial; }
.md-comment { color: rgb(162, 127, 3); opacity: 0.8; font-family: var(--monospace); }
code { text-align: left; }
a.md-print-anchor { white-space: pre !important; border: none !important; display: inline-block !important; position: absolute !important; width: 1px !important; right: 0px !important; outline: 0px !important; text-shadow: initial !important; background-position: 0px 0px !important; background-repeat: initial initial !important; }
.md-inline-math .MathJax_SVG .noError { display: none !important; }
.html-for-mac .inline-math-svg .MathJax_SVG { vertical-align: 0.2px; }
.md-math-block .MathJax_SVG_Display { text-align: center; margin: 0px; position: relative; text-indent: 0px; max-width: none; max-height: none; min-height: 0px; min-width: 100%; width: auto; overflow-y: hidden; display: block !important; }
.MathJax_SVG_Display, .md-inline-math .MathJax_SVG_Display { width: auto; margin: inherit; display: inline-block !important; }
.MathJax_SVG .MJX-monospace { font-family: var(--monospace); }
.MathJax_SVG .MJX-sans-serif { font-family: sans-serif; }
.MathJax_SVG { display: inline; font-style: normal; font-weight: 400; line-height: normal; zoom: 90%; text-indent: 0px; text-align: left; text-transform: none; letter-spacing: normal; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; }
.MathJax_SVG * { transition: none; }
.MathJax_SVG_Display svg { vertical-align: middle !important; margin-bottom: 0px !important; }
.os-windows.monocolor-emoji .md-emoji { font-family: "Segoe UI Symbol", sans-serif; }
.md-diagram-panel > svg { max-width: 100%; }
[lang="mermaid"] svg, [lang="flow"] svg { max-width: 100%; }
[lang="mermaid"] .node text { font-size: 1rem; }
table tr th { border-bottom-width: 0px; }
video { max-width: 100%; display: block; margin: 0px auto; }
iframe { max-width: 100%; width: 100%; border: none; }
.highlight td, .highlight tr { border: 0px; }


.CodeMirror { height: auto; }
.CodeMirror.cm-s-inner { background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror-scroll { overflow-y: hidden; overflow-x: auto; z-index: 3; }
.CodeMirror-gutter-filler, .CodeMirror-scrollbar-filler { background-color: rgb(255, 255, 255); }
.CodeMirror-gutters { border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; white-space: nowrap; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror-linenumber { padding: 0px 3px 0px 5px; text-align: right; color: rgb(153, 153, 153); }
.cm-s-inner .cm-keyword { color: rgb(119, 0, 136); }
.cm-s-inner .cm-atom, .cm-s-inner.cm-atom { color: rgb(34, 17, 153); }
.cm-s-inner .cm-number { color: rgb(17, 102, 68); }
.cm-s-inner .cm-def { color: rgb(0, 0, 255); }
.cm-s-inner .cm-variable { color: rgb(0, 0, 0); }
.cm-s-inner .cm-variable-2 { color: rgb(0, 85, 170); }
.cm-s-inner .cm-variable-3 { color: rgb(0, 136, 85); }
.cm-s-inner .cm-string { color: rgb(170, 17, 17); }
.cm-s-inner .cm-property { color: rgb(0, 0, 0); }
.cm-s-inner .cm-operator { color: rgb(152, 26, 26); }
.cm-s-inner .cm-comment, .cm-s-inner.cm-comment { color: rgb(170, 85, 0); }
.cm-s-inner .cm-string-2 { color: rgb(255, 85, 0); }
.cm-s-inner .cm-meta { color: rgb(85, 85, 85); }
.cm-s-inner .cm-qualifier { color: rgb(85, 85, 85); }
.cm-s-inner .cm-builtin { color: rgb(51, 0, 170); }
.cm-s-inner .cm-bracket { color: rgb(153, 153, 119); }
.cm-s-inner .cm-tag { color: rgb(17, 119, 0); }
.cm-s-inner .cm-attribute { color: rgb(0, 0, 204); }
.cm-s-inner .cm-header, .cm-s-inner.cm-header { color: rgb(0, 0, 255); }
.cm-s-inner .cm-quote, .cm-s-inner.cm-quote { color: rgb(0, 153, 0); }
.cm-s-inner .cm-hr, .cm-s-inner.cm-hr { color: rgb(153, 153, 153); }
.cm-s-inner .cm-link, .cm-s-inner.cm-link { color: rgb(0, 0, 204); }
.cm-negative { color: rgb(221, 68, 68); }
.cm-positive { color: rgb(34, 153, 34); }
.cm-header, .cm-strong { font-weight: 700; }
.cm-del { text-decoration: line-through; }
.cm-em { font-style: italic; }
.cm-link { text-decoration: underline; }
.cm-error { color: red; }
.cm-invalidchar { color: red; }
.cm-constant { color: rgb(38, 139, 210); }
.cm-defined { color: rgb(181, 137, 0); }
div.CodeMirror span.CodeMirror-matchingbracket { color: rgb(0, 255, 0); }
div.CodeMirror span.CodeMirror-nonmatchingbracket { color: rgb(255, 34, 34); }
.cm-s-inner .CodeMirror-activeline-background { background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror { position: relative; overflow: hidden; }
.CodeMirror-scroll { height: 100%; outline: 0px; position: relative; box-sizing: content-box; background-image: inherit; background-size: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: inherit; background-position: inherit inherit; background-repeat: inherit inherit; }
.CodeMirror-sizer { position: relative; }
.CodeMirror-gutter-filler, .CodeMirror-hscrollbar, .CodeMirror-scrollbar-filler, .CodeMirror-vscrollbar { position: absolute; z-index: 6; display: none; }
.CodeMirror-vscrollbar { right: 0px; top: 0px; overflow: hidden; }
.CodeMirror-hscrollbar { bottom: 0px; left: 0px; overflow: hidden; }
.CodeMirror-scrollbar-filler { right: 0px; bottom: 0px; }
.CodeMirror-gutter-filler { left: 0px; bottom: 0px; }
.CodeMirror-gutters { position: absolute; left: 0px; top: 0px; padding-bottom: 30px; z-index: 3; }
.CodeMirror-gutter { white-space: normal; height: 100%; box-sizing: content-box; padding-bottom: 30px; margin-bottom: -32px; display: inline-block; }
.CodeMirror-gutter-wrapper { position: absolute; z-index: 4; border: none !important; background-position: 0px 0px !important; background-repeat: initial initial !important; }
.CodeMirror-gutter-background { position: absolute; top: 0px; bottom: 0px; z-index: 4; }
.CodeMirror-gutter-elt { position: absolute; cursor: default; z-index: 4; }
.CodeMirror-lines { cursor: text; }
.CodeMirror pre { border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; border-width: 0px; font-family: inherit; font-size: inherit; margin: 0px; white-space: pre; word-wrap: normal; color: inherit; z-index: 2; position: relative; overflow: visible; background-position: 0px 0px; background-repeat: initial initial; }
.CodeMirror-wrap pre { word-wrap: break-word; white-space: pre-wrap; word-break: normal; }
.CodeMirror-code pre { border-right-width: 30px; border-right-style: solid; border-right-color: transparent; width: fit-content; }
.CodeMirror-wrap .CodeMirror-code pre { border-right-style: none; width: auto; }
.CodeMirror-linebackground { position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; z-index: 0; }
.CodeMirror-linewidget { position: relative; z-index: 2; overflow: auto; }
.CodeMirror-wrap .CodeMirror-scroll { overflow-x: hidden; }
.CodeMirror-measure { position: absolute; width: 100%; height: 0px; overflow: hidden; visibility: hidden; }
.CodeMirror-measure pre { position: static; }
.CodeMirror div.CodeMirror-cursor { position: absolute; visibility: hidden; border-right-style: none; width: 0px; }
.CodeMirror div.CodeMirror-cursor { visibility: hidden; }
.CodeMirror-focused div.CodeMirror-cursor { visibility: inherit; }
.cm-searching { background-color: rgba(255, 255, 0, 0.4); background-position: initial initial; background-repeat: initial initial; }
@media print { 
  .CodeMirror div.CodeMirror-cursor { visibility: hidden; }
}


@include-when-export url(https://fonts.loli.net/css?family=Ubuntu:400,700,400italic,700italic);
@include-when-export url(https://fonts.loli.net/css?family=Raleway:600,400&subset=latin,latin-ext);

@font-face {
	font-family: 'TeXGyreAdventor';
	font-style: normal;
	font-weight: normal;
	src: url(file:///Users/lx/Library/Application%20Support/abnerworks.Typora/themes/gothic/texgyreadventor-regular.woff) format('woff');
}
@font-face {
	font-family: 'TeXGyreAdventor';
	font-style: normal;
	font-weight: bold;
	src: url(file:///Users/lx/Library/Application%20Support/abnerworks.Typora/themes/gothic/texgyreadventor-bold.woff) format('woff');
}
@font-face {
	font-family: 'TeXGyreAdventor';
	font-style: italic;
	font-weight: normal;
	src: url(file:///Users/lx/Library/Application%20Support/abnerworks.Typora/themes/gothic/texgyreadventor-italic.woff) format('woff');
}
@font-face {
	font-family: 'TeXGyreAdventor';
	font-style: italic;
	font-weight: bold;
	src: url(file:///Users/lx/Library/Application%20Support/abnerworks.Typora/themes/gothic/texgyreadventor-bolditalic.woff) format('woff');
}

:root {
	--window-border: none;
	--typora-center-window-title: true;
	--active-file-bg-color: #f3f3f3;
	--bg-color: #fcfcfc;
	--control-text-color: #777;
}

.mac-seamless-mode #typora-sidebar {
	top: 20px;
	padding-top: 0;
	height: auto;
}

html,
body,
#write{
	background: #fcfcfc;
	background: var(--bg-color);
	font-family: 'TeXGyreAdventor', "Century Gothic", 'Yu Gothic', 'Raleway', sans-serif;
	font-weight: 300;
}
h1,
h2,
h3,
h4,
h5,
h6 {
	color: #111;
	font-family: 'TeXGyreAdventor', "Century Gothic", 'Yu Gothic', 'Ubuntu', sans-serif;
}

html {
	font-size:16px;
}

#write {
	max-width: 914px;
	text-align: justify;
}

#write>h1:first-child {
	margin-top: 2.75rem;
}
#write>h2:first-child {
	margin-top: 1.75rem;
}
#write>h3:first-child {
	margin-top: 1rem;
}
#write>h4:first-child {
	margin-top: 0.5rem;
}
h1 {
	font-weight: normal;
	line-height: 4rem;
	margin: 0 0 1.75rem;
	padding: 20px 30px;
	text-align: center;
	text-transform: uppercase;
	margin-top: 4rem;
}
h2 {
	font-weight: normal;
	line-height: 3rem;
	margin: 0 0 1.9375rem;
	padding: 0 30px;
	text-align: center;
	text-transform: uppercase;
	margin-top: 3rem
}
h3 {
	font-weight: normal;
}
h4 {
	font-weight: normal;
}
h5 {
	font-size: 1.125rem;
	font-weight: normal;
}
h6 {
	font-size: 1rem;
	font-weight: bold;
}
p {
	color: #111;
	font-size: 1rem;
	line-height: 1.75rem;
	margin: 0 0 1.25rem;
}
#write>h3.md-focus:before {
	left: -1.875rem;
	top: 0.5rem;
	padding: 2px;
}
#write>h4.md-focus:before {
	left: -1.875rem;
	top: 0.3125rem;
	padding: 2px;
}
#write>h5.md-focus:before {
	left: -1.875rem;
	top: 0.25rem;
	padding: 2px;
}
#write>h6.md-focus:before {
	left: -1.875rem;
	top: .125rem;
	padding: 2px;
}
@media screen and (max-width: 48em) {
	blockquote {
		margin-left: 1rem;
		margin-right: 0;
		padding: 0.5em;
	}
	.h1,
	h1 {
		font-size: 2.827rem;
	}
	.h2,
	h2 {
		font-size: 1.999rem;
	}
	.h3,
	h3 {
		font-size: 1.413rem;
	}
	.h4,
	h4 {
		font-size: 1.250rem;
	}
	.h5,
	h5 {
		font-size: 1.150rem;
	}
	.h6,
	h6 {
		font-size: 1rem;
	}
}
a,
.md-def-url {
	color: #990000;
	text-decoration: none;
}
a:hover {
	text-decoration: underline;
}
table {
	margin-bottom: 20px
}
table th,
table td {
	padding: 8px;
	line-height: 1.25rem;
	vertical-align: top;
	border-top: 1px solid #ddd
}
table th {
	font-weight: bold
}
table thead th {
	vertical-align: bottom
}
table caption+thead tr:first-child th,
table caption+thead tr:first-child td,
table colgroup+thead tr:first-child th,
table colgroup+thead tr:first-child td,
table thead:first-child tr:first-child th,
table thead:first-child tr:first-child td {
	border-top: 0
}
table tbody+tbody {
	border-top: 2px solid #ddd
}
code, .md-fences {
	padding: .5em;
	/*background: #f0f0f0;*/
	border: 1px solid #ccc;
	padding: .1em;
	font-size: 0.9em;
	margin-left: 0.2em;
	margin-right: 0.2em;
}
.md-fences {
	margin: 0 0 20px;
	font-size: 1em;
	padding: 0.3em 1em;
  	padding-top: 0.4em;
}
.task-list{
	padding-left: 0;
}

.task-list-item {
	padding-left:2.125rem;
}

input {
	border-style: ridge;
}

.task-list-item input {
	top: 3px;
}

.md-task-list-item input {
	margin-left: -1.5em;
}

.task-list-item input:before {
	content: "";
	display: inline-block;
	width: 1rem;
	height: 1rem;
	vertical-align: middle;
	text-align: center;
	border: 1px solid gray;
	background-color: #fdfdfd;
	margin-left: 0;
	-webkit-appearance: none;
	margin-top: -1rem;
}

.typora-node .task-list-item input:before {
	top: 0.3ex;
	position: absolute;
}

.task-list-item input:checked:before,
.task-list-item input[checked]:before{
	content: '\25FC';
	/*◘*/
	font-size: 0.8125rem;
	line-height: 0.9375rem;
}

/* Chrome 29+ */
@media screen and (-webkit-min-device-pixel-ratio:0)
  and (min-resolution:.001dpcm) {
    .task-list-item input:before {
    	margin-top: -0.2rem;
    }

    .task-list-item input:checked:before,
	.task-list-item input[checked]:before {
		margin-top: -0.2rem;
	}
}

blockquote {
	margin: 0 0 1.11111rem;
	padding: 0.5rem 1.11111rem 0 1.05556rem;
	border-left: 1px solid gray;
}
blockquote,
blockquote p {
	line-height: 1.6;
	color: #6f6f6f;
}
#write pre.md-meta-block {
	min-height: 30px;
	background: #f8f8f8;
	padding: 1.5em;
	font-weight: 300;
	font-size: 1em;
	padding-bottom: 1.5em;
	padding-top: 3em;
    margin-top: -1.5em;
	color: #999;
	
	width: 100vw;
	max-width: calc(100% + 60px);
	margin-left: -30px;
	border-left: 30px #f8f8f8 solid;
	border-right: 30px #f8f8f8 solid;
}
.MathJax_Display {
	font-size: 0.9em;
	margin-top: 0.5em;
	margin-bottom: 0;
}
p.mathjax-block,
.mathjax-block {
	padding-bottom: 0;
}
.mathjax-block>.code-tooltip {
	bottom: 5px;
	box-shadow: none;
}
.md-image>.md-meta {
	padding-left: 0.5em;
	padding-right: 0.5em;
}
.md-image>img {
	margin-top: 2px;
}
.md-image>.md-meta:first-of-type:before {
	padding-left: 4px;
}

#typora-source {
	color: #555;
}

/** ui for windows **/
#md-searchpanel {
    border-bottom: 1px solid #ccc;
}

#md-searchpanel .btn {
    border: 1px solid #ccc;
}

#md-notification:before {
	top: 14px;
}

#md-notification {
	background: #eee;
}

.megamenu-menu-panel .btn {
	border: 1px solid #ccc;
}

#typora-sidebar {
	box-shadow: none;
}

.file-list-item ,
.show-folder-name .file-list-item {
	padding-top: 20px;
	padding-bottom: 20px;
	line-height: 20px;
}

.file-list-item-summary {
	height: 40px;
	line-height: 20px;
}

.ty-table-edit {
	background: #ededed;
}

.dropdown-menu .divider {
	border-color: #e5e5e5;
}

 .typora-export li, .typora-export p, .typora-export,  .footnote-line {white-space: normal;} 
</style>
</head>
<body class='typora-export' >
<div  id='write'  class = 'is-mac'><h1><a name='header-n0' class='md-header-anchor '></a>R 编程基础学习笔记</h1><h2><a name='header-n2' class='md-header-anchor '></a>DAY 1</h2><h3><a name='header-n3' class='md-header-anchor '></a>RStudio的操作界面：</h3><ul><li>左上角——脚本编辑器（输入、运行、保存代码）；</li><li>左下角——控制台（也叫交互式命令运行窗口,输入命令,查看反馈结果和错误提醒）；</li><li>右上角——环境面板（保存之前生成的对象,包括向量和自定义函数等,也可供于查看）</li><li>右下角——Files（R保存的文件）,Plots（R生成的图像,可导出和删除）,Packages(安装的包),Help（帮助文档）</li></ul><h3><a name='header-n13' class='md-header-anchor '></a>RStudio的基础操作：</h3><ul><li>创建新项目(new project)</li><li>创建新脚本（new script）</li><li>创建对象并赋值（num&lt;-1）,run/ctrl+enter 运行代码；赋值也可以用等号（char=&quot;hello&quot;）,字符型可以是双引号也可以是单引号（char1&lt;-‘hi’）</li><li>删除对象（环境面板选中对象,点击扫把）</li><li>多行程序一起运行（选择多行代码,点击RUN或者使用快捷键ctrl+shift+enter）</li><li>读取变量的值（环境面板直接查看/输入对象的名字,直接运行/print(对象名称),运行）</li><li>变量的命名法则（不可以数字开头命名变量；可以以点开头命名变量,但是点后面要跟字母,不能用数字；变量命名区分大小写；不要用特殊词汇命名变量。）</li><li>注释（#注释内容）,多行注释（ctrl+shift+C,同样可以取消多行注释/菜单栏Code下拉选择comment/uncomment lines）</li><li>修改过去运行的代码（控制台用上下键切换过去运行的代码进行修改）</li><li>代码补全功能（输入函数名的前几个字母按Tab键；在函数括号中按Tab键,可以查看参数）</li><li>查看当前工作空间存在的对象（环境面板直接查看/运行ls()函数）</li><li>删除对象函数（环境面板,点击grid,选中对象,点击扫把/运行rm(想要删除的对象名)）</li><li>[1] 的含义：向量的下标是1,长度为1的向量</li><li>保存代码</li></ul><h3><a name='header-n43' class='md-header-anchor '></a>R的包</h3><ul><li>查看已加载的包——控制台输入运行search()函数</li><li>安装包——运行install.packages(&quot;包的名称&quot;),注意包的名称要加双引号/在右下角Packages直接点击Install</li><li>加载包——运行library(包的名称)</li><li>移除包——运行detach(&quot;package:包的名称&quot;)</li><li>了解包,寻求帮助——运行help(函数名称)/？函数名称,运行/右下角&quot;放大镜&quot;处搜索</li></ul><h2><a name='header-n55' class='md-header-anchor '></a>DAY2</h2><h3><a name='header-n56' class='md-header-anchor '></a>数据类型</h3><p>数值型/逻辑型/字符型/缺省值/浮点型/复数型/整数型</p><ul><li>创建字符型对象（向量）<span>	</span>char&lt;-&quot;hello&quot;（可以是双引号也可以是单引号）</li><li>创建逻辑型对象<span>	</span>logit&lt;-true</li><li>创建复数型对象<span>	</span>comp&lt;-2+2i （注意实部和虚部,虚部用i来表示）</li><li>数值型对象分整数型和浮点型,浮点型精度大（15位有效数字）,能够表示的范围更广泛（-10^308~10^308）,占用内存大；R语言默认数据类型为浮点型。保留整点型方便与其他语言交互。</li><li>创建浮点型数据<span>	</span>dou&lt;-1.11</li><li>创建整数型数据<span>	</span>int&lt;-3L (整数型要在数值后面加上L,不加则为浮点型)</li></ul><ul><li>查看数据类型函数<span>	</span>class(数据名称)</li><li>查看数据具体类型<span>	</span>typeof(数据类型) </li><li>如typeof(dou) 显示&quot;double&quot;,表示数据为浮点型</li><li>查看对象是否为某种类型</li><li>is.numeric(对象名称)<span>	</span>查看对象是否为数值型</li><li>is.character()<span>	</span>查看对象是否为字符型</li><li>is.logical()<span>	</span>查看对象是否为逻辑型</li><li>is.double()<span>	</span>查看对象是否为浮点型</li><li>is.na()<span>	</span>查看对象是否为缺失值的类型</li><li>is.complex()<span>	</span>查看对象是否为复合型</li><li>运行结果为TRUE/FALSE</li></ul><p>数据类型的转换</p><ul><li>as.character(转换对象的名称)<span>	</span>将对象转换为字符型</li><li>as.numeric()<span>	</span>将对象转换为数值型</li><li>as.logical()<span>	</span>将对象转换为逻辑型；转换0的时候会转换成FALSE,其他数值会自动转换为TRUE</li></ul><p>几个需要记忆的特殊数值</p><ul><li>Inf<span>	</span>正无穷</li><li>-Inf<span>	</span>负无穷</li><li>NaN<span>	</span>不可能的数</li><li>NA<span>	</span>缺失值的数</li><li>以上几个也有相应判断数值的函数用法,如is.infinite(a),即判断a是否为无穷</li><li>NULL<span>	</span>表示对象并未被赋值</li></ul><h3><a name='header-n117' class='md-header-anchor '></a>数据结构</h3><h4><a name='header-n118' class='md-header-anchor '></a>向量</h4><p>创建向量</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">B</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-number">1</span>,<span class="cm-number">2</span>,<span class="cm-number">3</span>,<span class="cm-number">4</span>,<span class="cm-number">5</span>,<span class="cm-number">6</span>,<span class="cm-number">7</span>)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 数值型</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">charb</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-string">"a"</span>,<span class="cm-string">"b"</span>,<span class="cm-string">"c"</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 字符型</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">logitb</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-variable">TRUE</span>,<span class="cm-variable">TRUE</span>,<span class="cm-variable">FALSE</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 逻辑型</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">num</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">10</span><span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 创建一个从1到10连续的向量</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">seq</span>(开始值,结束值,间隔)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 创建有间隔的连续向量</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">rep</span>(重复数值,<span class="cm-variable">times</span><span class="cm-operator">=</span>重复次数)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 创建重复向量</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 138px;"></div><div class="CodeMirror-gutters" style="display: none; height: 138px;"></div></div></div></pre><p>访问向量中的某个元素（R的下标从1开始）</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">向量名[元素在向量下标的位置]</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">B</span>[<span class="cm-number">1</span>]<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 显示B的第一个元素的值 1</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">用逻辑向量查看向量中某个指定位置元素的值</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">B</span>[<span class="cm-variable">c</span>(<span class="cm-variable">T</span>,<span class="cm-variable">F</span>,<span class="cm-variable">F</span>,<span class="cm-variable">F</span>,<span class="cm-variable">F</span>,<span class="cm-variable">F</span>)]<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 输出B中第一元素的值</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">删除向量中的元素</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">B</span>[<span class="cm-operator">-</span><span class="cm-number">1</span>]<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 删除B中第一个元素</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 184px;"></div><div class="CodeMirror-gutters" style="display: none; height: 184px;"></div></div></div></pre><h4><a name='header-n123' class='md-header-anchor '></a>矩阵</h4><p>创建矩阵</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">matrix</span>(元素的值,<span class="cm-variable">nrow</span><span class="cm-operator">=</span>行数,<span class="cm-variable">ncol</span><span class="cm-operator">=</span>列数,<span class="cm-variable">byrow</span><span class="cm-operator">=</span><span class="cm-variable">T</span><span class="cm-operator">/</span><span class="cm-variable">F</span>排列方法(默认为按列填充),行列名<span class="cm-variable">dimnames</span><span class="cm-operator">=</span><span class="cm-builtin">list</span>())</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">matrix1</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">matrix</span>(<span class="cm-number">1</span>：<span class="cm-number">6</span>,<span class="cm-variable">nrow</span><span class="cm-operator">=</span><span class="cm-number">2</span>,<span class="cm-variable">ncol</span><span class="cm-operator">=</span><span class="cm-number">3</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">matrix2</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">matrix</span>(<span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">6</span>,<span class="cm-variable">nrow</span><span class="cm-operator">=</span><span class="cm-number">2</span>,<span class="cm-variable">ncol</span><span class="cm-operator">=</span><span class="cm-number">3</span>,<span class="cm-variable">byrow</span><span class="cm-operator">=</span><span class="cm-variable">true</span>,<span class="cm-variable">dimnames</span><span class="cm-operator">=</span><span class="cm-builtin">list</span>(<span class="cm-variable">c</span>(<span class="cm-string">"r1"</span>,<span class="cm-string">"r2"</span>),<span class="cm-variable">c</span>(<span class="cm-string">"c1"</span>,<span class="cm-string">"c2"</span>,<span class="cm-string">"c3"</span>)))</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 115px;"></div><div class="CodeMirror-gutters" style="display: none; height: 115px;"></div></div></div></pre><p>访问矩阵</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">matrix2</span>[<span class="cm-number">2</span>,<span class="cm-number">3</span>]<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 查看matrix2中第二行,第二列的值</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">matrix2</span>[<span class="cm-string">"r2"</span>,<span class="cm-string">"c3"</span>]<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 查看matrix2中r2行,c3列的值</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">matrix2</span>[<span class="cm-number">2</span>,]<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 查看matrix2中第二行的值</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">给矩阵中某个元素赋值</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">matrix2</span>[<span class="cm-number">2</span>,<span class="cm-number">3</span>]<span class="cm-operator cm-arrow">&lt;-</span><span class="cm-number">10</span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 将matrix2矩阵的第二行第三列的元素赋值为10</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 115px;"></div><div class="CodeMirror-gutters" style="display: none; height: 115px;"></div></div></div></pre><h2><a name='header-n128' class='md-header-anchor '></a>DAY3</h2><h3><a name='header-n129' class='md-header-anchor '></a>数组</h3><p>创建数组</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">array</span>(<span class="cm-variable">data</span>, <span class="cm-variable">dim</span><span class="cm-operator">=</span><span class="cm-variable">length</span>(<span class="cm-variable">data</span>), <span class="cm-variable">dimnames</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">创建一个<span class="cm-number">1</span>到<span class="cm-number">5</span>的一维数组</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">arr1</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">array</span>(<span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">5</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">创建一个从<span class="cm-number">1</span>到<span class="cm-number">10</span>两行五列的二维数组</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">arr2</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">array</span>(<span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">10</span>,<span class="cm-variable">dim</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-number">2</span>,<span class="cm-number">5</span>))</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">创建一个含有名称的三维数组</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">gender</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-string">"男"</span>,<span class="cm-string">"女"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">summarys</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-string">"average_height"</span>,<span class="cm-string">"average_weight"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">classes</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-string">"class1"</span>,<span class="cm-string">"class2"</span>) &nbsp;<span class="cm-variable">arr3</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">array</span>(<span class="cm-variable">c</span>(<span class="cm-number">178</span>,<span class="cm-number">165</span>,<span class="cm-number">70</span>,<span class="cm-number">55</span>,<span class="cm-number">175</span>,<span class="cm-number">164</span>,<span class="cm-number">77</span>,<span class="cm-number">50</span>),<span class="cm-variable">dim</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-number">2</span>,<span class="cm-number">2</span>,<span class="cm-number">2</span>),<span class="cm-variable">dimnames</span><span class="cm-operator">=</span><span class="cm-builtin">list</span>(<span class="cm-variable">gender</span>,<span class="cm-variable">summarys</span>,<span class="cm-variable">classes</span>))</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 322px;"></div><div class="CodeMirror-gutters" style="display: none; height: 322px;"></div></div></div></pre><p>读取数组中的元素</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">arr3</span>[<span class="cm-number">2</span>,<span class="cm-number">2</span>,<span class="cm-number">1</span>]</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">arr3</span>[<span class="cm-string">"女"</span>,<span class="cm-string">"average_weight"</span>,<span class="cm-string">"class1"</span>]</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 46px;"></div><div class="CodeMirror-gutters" style="display: none; height: 46px;"></div></div></div></pre><h3><a name='header-n134' class='md-header-anchor '></a>数据框</h3><p>数据框的不同维度可以有不同的数据类型</p><p>创建数据框</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">data.frame</span>()</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">student</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">data.frame</span>(<span class="cm-variable">ID</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-number">111</span>,<span class="cm-number">112</span>,<span class="cm-number">112</span>),<span class="cm-variable">name</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-string">"lily"</span>,<span class="cm-string">"bob"</span>,<span class="cm-string">"sam"</span>),<span class="cm-variable">gender</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-string">"F"</span>,<span class="cm-string">"M"</span>,<span class="cm-string">"M"</span>),<span class="cm-variable">stringsasfactors</span><span class="cm-operator">=</span><span class="cm-variable">F</span>(此处如果添加此语句则将数据类型从因子型变为字符型))</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 92px;"></div><div class="CodeMirror-gutters" style="display: none; height: 92px;"></div></div></div></pre><p>访问数据框中的元素</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">student</span>[<span class="cm-number">1</span>,<span class="cm-number">1</span>]<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 访问第一行第一列的元素</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">student</span>[<span class="cm-string">"ID"</span>]<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 访问ID列元素</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">student</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">ID</span><span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 访问ID列元素</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 69px;"></div><div class="CodeMirror-gutters" style="display: none; height: 69px;"></div></div></div></pre><h3><a name='header-n140' class='md-header-anchor '></a>因子</h3><ul><li>变量可以分为分类变量（名义型分类变量（如红黄蓝绿,男女）和有序型分类变量（如优良中差,表示顺序关系））和连续型变量（某个范围里的任意值,可以表示顺序和数量,如年龄）</li><li>因子起分类变量的作用,可分为有序型和无序型</li><li>gender=c(&quot;F&quot;,&quot;M&quot;,&quot;M&quot;,&quot;F&quot;,&quot;F&quot;)</li><li>factorgender&lt;-factor(gender)</li><li>查看因子向量水平<span>	</span>levels()</li></ul><h3><a name='header-n152' class='md-header-anchor '></a>列表</h3><ul><li>列表中的各个元素的类型和长度可以不同</li><li>列表有长度没有维度</li></ul><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R" style="page-break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 创建列表</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-builtin">list</span>()</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list1</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-builtin">list</span>(<span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">6</span>,<span class="cm-variable">letters</span>[<span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">10</span>],<span class="cm-variable">student</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 此列表没有名称,只能用嵌套的[]来访问元素</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list1</span>[[<span class="cm-number">1</span>]]</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 创建有列表名称的列表</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list2</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-builtin">list</span>(<span class="cm-variable">a</span><span class="cm-operator">=</span><span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">10</span>,<span class="cm-variable">b</span><span class="cm-operator">=</span><span class="cm-variable">arr3</span>,<span class="cm-variable">c</span><span class="cm-operator">=</span><span class="cm-variable">student</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 访问元素</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list2</span>[[<span class="cm-number">2</span>]]</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list2</span>[[<span class="cm-string">"b"</span>]]</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list2</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">b</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看列表长度</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">length</span>(<span class="cm-variable">list2</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 添加元素</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list2</span>[[<span class="cm-number">4</span>]]<span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">list1</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list2</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">d</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">100</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 合并列表</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list3</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-variable">list1</span>,<span class="cm-variable">list2</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 修改列表元素</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list1</span>[[<span class="cm-number">1</span>]][<span class="cm-number">1</span>]<span class="cm-operator cm-arrow">&lt;-</span><span class="cm-number">0</span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 将list1中第一个表的第一个元素改为0</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 将向量转换为列表</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">v1</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">10</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">list4</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">as.list</span>(<span class="cm-variable">v1</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 将列表转换为向量</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">unlist</span>(<span class="cm-variable">list4</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 736px;"></div><div class="CodeMirror-gutters" style="display: none; height: 736px;"></div></div></div></pre><h2><a name='header-n159' class='md-header-anchor '></a>DAY4</h2><h3><a name='header-n160' class='md-header-anchor '></a>导入文本（xls,csv,txt）</h3><h4><a name='header-n161' class='md-header-anchor '></a>导入csv文件</h4><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 安装readr包</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">install.packages</span>(<span class="cm-string">"readr"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 导入包</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">library</span>(<span class="cm-variable">readr</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 导入csv文件</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">read_csv</span>(<span class="cm-string">"文件路径"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 指定导入数据类型</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">read_csv</span>(<span class="cm-string">"文件路径"</span>,<span class="cm-variable">col_types</span><span class="cm-operator">=</span><span class="cm-variable">cols</span>(列的名称<span class="cm-operator">=</span><span class="cm-variable">col_character</span>(),列的名称 &nbsp;<span class="cm-operator">=</span><span class="cm-variable">col_character</span>(),……))<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 将原数据类型改为字符型</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># col_names参数</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">read_csv</span>(<span class="cm-string">"文件路径"</span>,<span class="cm-variable">col_names</span><span class="cm-operator">=</span><span class="cm-variable">T</span>)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 当col_names=T时,导入数据时,默认将数据集的第一行当作列名；若col_names=F时,数据集的第一行为观测值,列名为x1,x2……,且整个数据框的数据类型变为字符型</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 276px;"></div><div class="CodeMirror-gutters" style="display: none; height: 276px;"></div></div></div></pre><h4><a name='header-n163' class='md-header-anchor '></a>导入excel文件</h4><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 安装readxl包</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 导入包</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 导入excel数据</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">read_excel</span>(<span class="cm-string">"文件路径"</span>,<span class="cm-variable">col_names</span><span class="cm-operator">=</span><span class="cm-variable">T</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 默认为T,可写可不写</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 如果excel有两个sheet,默认导入第一个sheet</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 导入指定数据集</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">read_excel</span>(<span class="cm-string">"文件路径"</span>,<span class="cm-variable">sheet</span><span class="cm-operator">=</span><span class="cm-string">"sheet名称"</span>,<span class="cm-variable">range</span><span class="cm-operator">=</span><span class="cm-string">"A1:D10"</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 导入excel中指定sheet的指定范围内（A1到D10）的数据集</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 184px;"></div><div class="CodeMirror-gutters" style="display: none; height: 184px;"></div></div></div></pre><h4><a name='header-n165' class='md-header-anchor '></a>导入txt文件</h4><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 导入txt文件函数</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">read.csv</span>(<span class="cm-string">"文件路径"</span>,<span class="cm-variable">sep</span><span class="cm-operator">=</span><span class="cm-string">"文件中数据的分隔符"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 数据编辑函数</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">数据框名称<span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">edit</span>(数据框名称)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 注意：此函数一定是赋值到数据框本身,不然编辑的数据将会丢失</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 92px;"></div><div class="CodeMirror-gutters" style="display: none; height: 92px;"></div></div></div></pre><h3><a name='header-n167' class='md-header-anchor '></a>内置数据集/数据集的基本信息</h3><p>R语言中自带的经典数据集</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R" style="page-break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看R语言中自带的数据集</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">data</span>()</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看数据集中前几行的数据</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">head</span>(<span class="cm-variable">iris</span>,<span class="cm-number">10</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 查看自带数据集iris的前10行</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看数据集末尾几行的数据</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">tail</span>(<span class="cm-variable">iris</span>,<span class="cm-number">10</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 查看自带数据集iris的末尾10行</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 指定package名称</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">data</span>(<span class="cm-variable">package</span><span class="cm-operator">=</span><span class="cm-string">"package名称"</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 查看package中的可用数据集</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看对象内部结构</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">str</span>(数据集名称)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看数据集的名称</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">names</span>(数据集名称)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 返回数据集的列名</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">names</span>(数据集名称)[具体列数]<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 返回数据集某一列的名称</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看数据集的行数</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">nrow</span>(数据集名称)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看数据集的列数</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">ncol</span>(数据集名称)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看数据集的维度</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">dim</span>(数据集名称)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 返回数据集的行数和列数</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看数据集的长度</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">length</span>(数据集名称)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看数据集的特征</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">attributes</span>(数据集名称)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 返回数据集的列名,类型和行的名称,输出结果为list形式</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 查看数据集统计概要</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">summary</span>(数据集名称)</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 575px;"></div><div class="CodeMirror-gutters" style="display: none; height: 575px;"></div></div></div></pre><h3><a name='header-n170' class='md-header-anchor '></a>写入文件</h3><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 写入csv文件</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">write.csv</span>(写入数据集的名称,<span class="cm-string">"写入文件路径文件扩展名为csv"</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 文件会自动生成行的名称</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 如果不想加入行的名称：</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">write.csv</span>(写入数据集的名称,<span class="cm-string">"写入文件路径"</span>,<span class="cm-variable">rownames</span><span class="cm-operator">=</span><span class="cm-variable">F</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 写入txt文件</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">write.csv</span>(写入数据集的名称,<span class="cm-string">"写入文件路径文件扩展名为txt"</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 默认分隔符为逗号</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 将输出结果保存到文件</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">sink</span>(<span class="cm-string">"写入文件路径"</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 1. 将终端转入外部文件</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 2. 运行将要输出的内容</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">sink</span>()<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 3. 保存输出结果</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 此三步为一整套操作</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 276px;"></div><div class="CodeMirror-gutters" style="display: none; height: 276px;"></div></div></div></pre><h2><a name='header-n172' class='md-header-anchor '></a>DAY6 第五讲 基本函数（1）</h2><h3><a name='header-n173' class='md-header-anchor '></a>运算符</h3><p>算术运算符</p><ul><li>加+ 减- 乘* 除/ 幂^</li><li>求余数 a%%b</li></ul><p>比较运算符</p><ul><li>返回值为TRUE/FALSE</li><li>3&lt;2<span>	</span>FALSE</li><li>3&gt;2<span>	</span>TRUE</li><li>3==3(3恒等于3)<span>	</span>TRUE</li><li>3!=3(3不等于3)<span>	</span>FALSE</li><li>3&lt;=2<span>	</span>FALSE<span>	</span>&lt;=表示小于或等于</li><li>3&gt;=2<span>	</span>TRUE<span>	</span>&gt;=表示大于或等于</li></ul><p>逻辑运算符</p><ul><li>逻辑或</li><li>F|T<span>	</span>返回TRUE</li><li>T|T<span>	</span>返回TRUE</li><li>F|F<span>	</span>返回FALSE</li><li>逻辑与</li><li>F&amp;T<span>	</span>返回FALSE</li><li>T&amp;T<span>	</span>返回TRUE</li><li>F&amp;F<span>	</span>返回FALSE</li><li>逻辑非</li><li>!T<span>	</span>返回FALSE</li><li>!F<span>	</span>返回TRUE</li><li>e&lt;-c(T,T,F)</li><li>f&lt;-c(T,F,F)</li><li>e|f<span>	</span>返回TRUE  TRUE  FALSE</li><li>e&amp;f<span>	</span>返回TRUE  FLASE  FALSE</li><li>!e<span>	</span>返回FALSE  FALSE  TRUE</li></ul><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">a</span>[<span class="cm-variable">a</span><span class="cm-operator">&gt;</span><span class="cm-number">10</span><span class="cm-operator">&amp;</span><span class="cm-variable">a</span><span class="cm-operator">&lt;</span><span class="cm-number">20</span>]<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 找出a中大于10并小于20的数值</span></span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 23px;"></div><div class="CodeMirror-gutters" style="display: none; height: 23px;"></div></div></div></pre><h3><a name='header-n231' class='md-header-anchor '></a>数值处理函数</h3><p>数学函数</p><ul><li>求绝对值<span>	</span>abs()</li><li>求平方根<span>	</span>sqrt()</li><li>余弦函数<span>	</span>cos()</li><li>正弦函数<span>	</span>sin()</li><li>正切函数<span>	</span>tan()</li><li>自然对数<span>	</span>log()</li><li>指数函数<span>	</span>exp()</li><li>舍入指定位数<span>	</span>round(x,3) 将x舍入三位小数</li></ul><p>统计函数</p><ul><li>求平均数<span>	</span>mean()</li><li>中位数<span>	</span>median()</li><li>方差<span>	</span>var()</li><li>标准差<span>	</span>sd()</li><li>分位数<span>	</span>quantile()</li><li>取值范围<span>	</span>range()</li><li>最大值<span>	</span>max()</li><li>最小值<span>	</span>min()</li><li>求合<span>	</span>sum()</li></ul><h2><a name='header-n270' class='md-header-anchor '></a>DAY7 第6讲 基本函数（2）</h2><h3><a name='header-n271' class='md-header-anchor '></a>字符处理函数</h3><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R" style="page-break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">A</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-string">"I"</span>,<span class="cm-string">"like"</span>,<span class="cm-string">"R"</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">nchar</span>(<span class="cm-variable">A</span>)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 返回值1 4 1；向量中每个字符的长度</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">length</span>(<span class="cm-variable">A</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 返回值3；向量的长度</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">paste</span>(<span class="cm-string">"x"</span>,<span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">10</span>,<span class="cm-variable">sep</span><span class="cm-operator">=</span><span class="cm-string">"_"</span>,<span class="cm-variable">collapse</span><span class="cm-operator">=</span><span class="cm-string">";"</span>)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 将向量变为字符型并组合起来形成字符串,sep设置向量之间分隔符（向量长度为字符个数）,collapse设置连接符符号（向量长度为1）</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">date</span>()<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 返回当前时刻</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">strsplit</span>()<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># paste() 的逆操作,分割字符串,返回结果为列表形式</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">s</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-string">"I like R"</span>,<span class="cm-string">"me too"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">strsplit</span>(<span class="cm-variable">s</span>,<span class="cm-variable">split</span><span class="cm-operator">=</span><span class="cm-string">""</span>)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># split参数用于指定拆分位置,返回结果为列表结构,""为拆分为字母</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">strsplit</span>(<span class="cm-variable">s</span>,<span class="cm-variable">split</span><span class="cm-operator">=</span><span class="cm-string">"</span> <span class="cm-string">"</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># "(空格)"拆分为单词</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">substr</span>()</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">x</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-string">"abcdef"</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">substr</span>(<span class="cm-variable">x</span>,<span class="cm-number">1</span>,<span class="cm-number">3</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 提取/替换字符向量中的字符,x为向量名称,为起始位置,3为结束位置</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">substr</span>(<span class="cm-variable">x</span>,<span class="cm-number">1</span>,<span class="cm-number">3</span>)<span class="cm-operator cm-arrow">&lt;-</span><span class="cm-string">"uvwxyz"</span><span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 将向量x的第一到三位替换,因为"uvwxyz"超过了3位,所以取前三位替换,返回结果为"uvwdef"</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">sub</span>()</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">y</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-string">"baby"</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">sub</span>(<span class="cm-string">"b"</span>,<span class="cm-string">"B"</span>,<span class="cm-variable">y</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># "b"为需要替换的字符,"B"为替换成的字符,y为替换的变量名；返回值为替换后的字符("Baby"),但是该函数并不改变原始变量</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">gsub</span>()<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 替换所有匹配到的字符</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">gsub</span>(<span class="cm-string">"b"</span>,<span class="cm-string">"B"</span>,<span class="cm-variable">y</span>)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 返回值为"BaBy"</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">toupper</span>()<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 将字符转换为大写</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">tolower</span>()<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 将字符转换为小写</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 529px;"></div><div class="CodeMirror-gutters" style="display: none; height: 529px;"></div></div></div></pre><h3><a name='header-n273' class='md-header-anchor '></a>apply函数族</h3><p>apply(要被修改的数据框,维度,功能)<span>	</span>修改输出生成为一个新的数据</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">names</span>(<span class="cm-variable">air_data</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">xiugainames</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-string">"AGE"</span>,<span class="cm-string">"SUM_YR_1"</span>,<span class="cm-string">"SUM_YR_2"</span>,<span class="cm-string">"LAST_FLIGHT_DATA"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">air_data</span>[<span class="cm-variable">xiugainames</span>]<span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">apply</span>(<span class="cm-variable">air_data</span>[<span class="cm-variable">xiugainames</span>],<span class="cm-number">2</span>,<span class="cm-variable">as.numeric</span>)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 将四列改为数值型</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">apply</span>(<span class="cm-variable">air_data</span>[<span class="cm-variable">xiugainames</span>],<span class="cm-number">2</span>,<span class="cm-variable">class</span>)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 查看四列数据类型,2为按列查看,1为按行</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">apply</span>(<span class="cm-variable">air_data</span>[<span class="cm-variable">xiugainames</span>],<span class="cm-number">2</span>,<span class="cm-variable">mean</span>,<span class="cm-variable">na.rm</span><span class="cm-operator">=</span><span class="cm-variable">T</span>)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 清楚四列中的缺失值求每列的平均值</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">lapply</span>(数据集名称,函数名称)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 与apply相似,不同为可针对list进行操作</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">lapply</span>(<span class="cm-variable">air_data</span>,<span class="cm-variable">typeof</span>)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 查看数据类型</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">sapply</span>(数据集名称,函数名称)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 与 lapply() 类似,也针对list进行处理,会根据返回结果的类型和结构生产一个合适的数据类型</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">mapply</span>()<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># sapply() 的多变量版本,可以将函数运用到多个列表和多个向量上</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 230px;"></div><div class="CodeMirror-gutters" style="display: none; height: 230px;"></div></div></div></pre><h2><a name='header-n276' class='md-header-anchor '></a>DAY8 第7讲 控制流</h2><p>控制流：按照一定顺序排列程序元素,来决定程序执行的顺序。</p><h3><a name='header-n278' class='md-header-anchor '></a>循环语句：重复循环一个或一系列语句,直到某个条件不为真为止</h3><h4><a name='header-n279' class='md-header-anchor '></a>for 语句</h4><p>重复循环语句,直到变量值不在序列中为止</p><p>for(variable in vector){}</p><ul><li>variable为变量</li><li>vector 为序列</li><li>{}中为循环语句</li></ul><p>举例：</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">for</span> (<span class="cm-variable">i</span> <span class="cm-keyword">in</span> <span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">10</span>){<span class="cm-variable">print</span>(<span class="cm-variable">i</span>)}<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 输出1到10</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">s</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-number">0</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">for</span> (<span class="cm-variable">i</span> <span class="cm-keyword">in</span> <span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">50</span>){<span class="cm-variable">s</span><span class="cm-operator">=</span><span class="cm-variable">s</span><span class="cm-operator">+</span><span class="cm-variable">i</span>}<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 输出s的值,为1275,此函数效果等同于sum(1:50)</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 69px;"></div><div class="CodeMirror-gutters" style="display: none; height: 69px;"></div></div></div></pre><h4><a name='header-n291' class='md-header-anchor '></a>while 语句</h4><p>重复循环语句,直到条件不为真为止</p><p>while (condition) {}</p><ul><li>condition 为条件,当condition不为真时循环停止</li><li>{}中为循环语句</li></ul><p>举例：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n300" mdtype="fences">i&lt;-1
while(i&lt;=10) {print(i)
i=i+1}	# 输出1到10
   ②s&lt;-0
     i&lt;-0
     while (i&lt;=50) {s&lt;-s+i
i=i+1}	# 输出s的值,为1275
# 处理大型数据时,此两种方法比较费时,推荐使用apply族函数和R语言自带函数
</pre><h3><a name='header-n301' class='md-header-anchor '></a>条件语句</h3><h4><a name='header-n302' class='md-header-anchor '></a>if-else 语句</h4><p>if(condition){}</p><ul><li>condition 为条件</li><li>{}中为运行语句</li></ul><p>举例：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n310" mdtype="fences">if (i%%2==0){	# 判断i是否为偶数,如果是,则输出i是
print(paste(I,"是偶数"))	# 偶数
} else {print(paste(I,"是奇数"))}	# 如果条件不成立,则输出i是奇数
# 此处else也可以写成elseif,即当第一个条件为假的时候,再一次进行判断

grade&lt;-90
if (grade&lt;60) {
print("不及格")
}else if(grade&lt;90) {	# 此处条件即为如果grade&gt;=60且&lt;90
   print("良好")
}else {
   print("优秀")
}
</pre><h4><a name='header-n311' class='md-header-anchor '></a>ifelse 函数</h4><p>ifelse函数可以对向量进行操作</p><p>ifelse(条件,条件为真时执行语句,条件为假时执行语句)</p><p>举例：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n315" mdtype="fences" style="page-break-inside: unset;">i&lt;-1:10
ifelse(i%%2==0,"偶数","奇数")	# 如果i能整除2,则输出"偶数"；不能则输出"奇数" 

ifelse(grade&lt;60,"不及格",ifelse(grade&lt;90,"良好","优秀"))	# ifelse中可以多层嵌套

# switch结构：根据表达式的值决定输出序列中的哪一个值
switch(2,"first","second","third")	# 运行输出结果为"second"
#第一个逗号前的表达式（2）决定输出其逗号后面相应的元素
switch(10,"first","second","third")	# 如果表达式大于后面元素个数,运行则无输出结果
#还可以对后面元素进行赋值
switch(2,"first"=11,"second"=22,"third"=33)	# 运行输出结果为22
#R语言中常将多种结构结合使用
s&lt;-0
for(i in 1:50) {
if(i%%2==0) {
  s=s+i
}
   }
s	# 运行结果为650（判断i是否为偶数,如果是则相加,即50以内偶数的和）
# 等同于
sum(seq(0,50,2))	# 求和,起始数值为0,结束数值为50,间隔为2
</pre><h2><a name='header-n316' class='md-header-anchor '></a>DAY9 第8讲 自定义函数</h2><h3><a name='header-n317' class='md-header-anchor '></a>自定义函数、参数、返回值</h3><p>创建自定义函数：function(variables) {函数体}</p><p>举例①：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n320" mdtype="fences">myprint&lt;-function() {
   print("I like R!")
}
</pre><p>调用时： myprint()</p><p>运行输出：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="false" cid="n323" mdtype="fences">"I like R!"
</pre><p>注意：调用时一定要加圆括号,不然运行显示自定义函数的具体内容。</p><p>举例②：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n326" mdtype="fences">myadd&lt;-function(a=1,b=2) {
   print(paste("The first is",a))
   print(paste("The first is",b))
   return(a+b)
}
</pre><p>调用： myadd()</p><p>输出结果：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="false" cid="n329" mdtype="fences">"The first is 1"
"The first is 2"
3
</pre><p>调用： </p><p>myadd(a=10,b=11)</p><p>输出结果：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="false" cid="n333" mdtype="fences">"The first is 10"
"The second is 11"
21
</pre><p>调用：</p><p>a_and_b&lt;-myadd(a=10,b=11)</p><p>输出结果：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="false" cid="n337" mdtype="fences">"The first is 10"
"The first is 11"
</pre><p>此时,运行a_and_b,输出结果：</p><p>21</p><p>注意：例②中语句的顺序,如果将return(a+b)改为a+b并放在第一行,即</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n341" mdtype="fences">myadd&lt;-function(a=1,b=2) {
	return(a+b)
print(paste("The first is",a))
	print(paste("The second is",b))
}
</pre><p>则运行a_and_b,输出结果为:</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="" contenteditable="false" cid="n343" mdtype="fences">"The second is 11"。
</pre><p>例②中语句的顺序,如果将return(a+b)移到第一行,即:只运行第一行代码,输出结果为3。</p><p>因此注意返回值编写的顺序。</p><h3><a name='header-n346' class='md-header-anchor '></a>匿名函数</h3><p>匿名函数为没有名称的函数,常用作其他函数的参数。</p><p>举例：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n349" mdtype="fences">sapply(1:10,function(x) {x^2})	# 此处函数没有名称即为匿名函数。
# 输出结果为1到10的平方
# 直接使用匿名函数：
(function(x) {x^2}) (1:10)	# 此处使用匿名函数外面要加圆括号
# 求一列数的平方的另一种方法：
c(1:10)^2
</pre><h3><a name='header-n350' class='md-header-anchor '></a>例子</h3><p>①斐波那契数列</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n352" mdtype="fences" style="page-break-inside: unset;">v3&lt;c(1,1)
for (i in 3:x) {
	v3[i]=v3[i-1]+v3[i-2]
}
# 将其创建成一个函数：
Fibonacci&lt;-function(x) {
	v3&lt;c(1,1)
	for (i in 3:x) {
		v3[i]=v3[i-1]+v3[i-2]
	}
	return(v3)
}
# 调用运行Fibonacci(15) ,输出斐波那契数列前15个数。
# 当输入2时,则出现错误,对函数进行修改
Fibonacci&lt;-function(x) {
	v3&lt;c(1,1)
	if (x&lt;=2){
		return(v3[1:x])
	} else {
		for (i in 3:x) {
			v3[i]=v3[i-1]+v3[i-2]
		}
	return(v3)}
}
# 此时调用函数Fibonacci(2),则输出斐波那契数列前两位数。
</pre><p>②自定义函数练习</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n354" mdtype="fences">mysummary&lt;-function(x) {
	mean_=mean(x)
	sd_=sd(x)
	p_25=quantile(x,probs=0.25)
	median_=median(x)
	return(c(junzhi=mean_,fangcha=sd_,p_25,zhongweishu=median_))
}
# 运行调用函数
air_data_1&lt;-na.omit(air_data)	# 去除缺失值
mysummary(air_data$SUM_YR_1)
# 输出结果为均值,方差,下四分位数和中位数
</pre><h2><a name='header-n355' class='md-header-anchor '></a>DAY10 第9讲 基本数据操作（1）</h2><h3><a name='header-n356' class='md-header-anchor '></a>创建新变量</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n357" mdtype="fences" style="page-break-inside: unset;">air_narm&lt;-na.omit(air_data)	# 删除缺失值
names(air_narm)	# 查看列的名称
air_narm$SUM_YR&lt;-air_narm$SUM_YR_1+air_narm$SUM_YR_2
# 创建新变量,为SUM_YR_1 、SUM_YR_2两列之和
# 另一种方法：
air_narm["SUM_YR_12"]&lt;-air_narm["SUM_YR_1"]+air_narm["SUM_YR_2"]
# 用$和[]都可以返回变量,但是数据类型不同,$返回为向量,[]返回为数据框
# 按照乘客年龄创建一个新变量air_narm$YMO
air_narm$YMO&lt;- ifelse(air_narm$AGE&lt;30,"YOUNG",ifelse(air_narm$AGE&gt;60,"OLD","Middle"))
air_narm$YMO_2&lt;-"Middle"	# 先将YMO_2一列均设为Middle
air_narm$AGE&lt;30	# 输出值为逻辑变量
air_narm$YMO_2[air_narm$AGE&lt;30]&lt;-"YOUNG"
air_narm$YMO_2[air_narm$AGE&gt;60]&lt;-"OLD"
sum(air_narm$YMO!=air_narm$YMO_2)	# 验证两种方法完全一样
</pre><h3><a name='header-n358' class='md-header-anchor '></a>变量重命名</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n359" mdtype="fences">names(air_narm)[4]&lt;-"xingbie"	# 修改列的名字
names(air_narm)[c(1,3,4)]&lt;-c("c1","c3","c4")	# 修改向量的名字
xiugainame&lt;-c("ADD_Point_SUM","Points_Sum")
names(air_narm)%in%xiugainame	# 判断后面向量是否存在前面向量中,返回结果为逻辑向量
names(air_narm)[names(air_narm)%in%xiugainame]&lt;-c("C100","C200")
# 修改相应列名
</pre><h3><a name='header-n360' class='md-header-anchor '></a>提取子集</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n361" mdtype="fences" style="page-break-inside: unset;"># 保留/删除列
names(air_narm)	# 查看列的名称
baoliu&lt;-c("SUM_YR_2","BEGIN_TO_FIRST","P1Y_Flight_Count")
baoliudata&lt;-air_narm[baoliu]
保留"SUM_YR_2","BEGIN_TO_FIRST","P1Y_Flight_Count"三列
baoliudata_2&lt;-air_narm[,c(1,3,11)]	# 保留1,3,11列的变量
newdata&lt;-air_narm[,-c(1,2)]	# 去除第1,2列
# 编辑行
newdata_3&lt;-air_narm[c(1,5,7),]	# 赋值1,5,7三行,只有三个观测值,48个变量
newdata_4&lt;-air_narm[air_narm$YMO=="OLD",] 
输出结果为逻辑向量
view(newdata_4)	# 观测YMO为OLD的数据集
# subset()函数
newdata_5&lt;-subset(air_narm,YMO="OLD",)	# 第二个值为逻辑判断确定条件,选取YMO为OLD的行
newdata_6&lt;-subset(air_narm,,baoliu)	# 选取baoliu列
</pre><h2><a name='header-n362' class='md-header-anchor '></a>DAY11 第10讲 基本数据操作（2）</h2><h3><a name='header-n363' class='md-header-anchor '></a>数据集的合并</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n364" mdtype="fences" style="page-break-inside: unset;"># merge()函数
merge(x,y,by=intersect(names(x),names(y)))
# x,y为要合并的数据框
# by为提取x和y的列名,用共有列作为连接列
a&lt;-air_data[1:4,1:3]
b&lt;-air_data[3:7,c(1,4,5)]
merge(a,b,by="MEMBER_NO")	# 以MEMBER_NO为公共列
names(a)[1]="m"	# 将a数据集的第一列名称改为m
merge(a,b,by.x="m",by.y=" MEMBER_NO")
# 以x数据集（a）中的m 列和y数据集（b）中的MEMBER_NO列为公共列合并
merge(a,b,by.x="m",by.y=" MEMBER_NO",all=TRUE) 
# 将a的四行和b的三行合并；当all=TRUE时直接将两个数据集汇总,缺失的项为空白
merge(a,b,by.x="m",by.y=" MEMBER_NO",all.y=TRUE) 
# 返回y（b）中所有的行和x（a）中匹配的行
# cbind()函数
# 直接横向合并,不需要指定公共的列,但要求二者有相同的行数 ,并按一定顺序排序
e&lt;-air_data[1:4,1:3]
f&lt;-air_data[1:4,4:7]
cbind(e,f)	
air_data[1:4,1:7]
# rbind()函数
# 纵向合并,合并的两个数据集要有相同的变量名
g&lt;-airdata[10:11,1:3]
rbind(e,g)
</pre><h3><a name='header-n365' class='md-header-anchor '></a>删除重复观测值</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n366" mdtype="fences"># duplicated()函数
# 返回结果为逻辑向量,在使用函数删除重复观测值
e&lt;-air_data[1:4,1:3]
g&lt;-air_data[3:5,1:3]
h&lt;-rbind(e,g)
duplicated(h)
h[!duplicated(h),]
</pre><h3><a name='header-n367' class='md-header-anchor '></a>数据排序</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n368" mdtype="fences" style="page-break-inside: unset;"># sort()函数
age&lt;-air_data$AGE[1:10]
sort(age)	# 将age从小到大排列
sort(age,decreasing=T)	# 将数据按age从大到小排列
x&lt;-c(10,2,19,5,NA)
sort(x)	# 将NA剔除后排序
sort(x,na.last=T)	# 将NA排在最后一位
sort(x,na.last=F)	# 将NA排在第一位
sort(x,na.last=NA)	# 将NA值去掉
# order()函数
order(age)	# 将age从小到大排列,返回结果为元素的下标
age[order(age)]	# 将age从小到大排列
D&lt;-air_data[order(air_data$SEG_KM_SUM),]
# 将数据集的数据按照SEG_KM_SUM从小到大的顺序排序
# rank()函数
rank(age)	# 输出结果为当前向量中各元素从小到大的排序,即向量中34这个值对应输出结果为2,即23为向量中第二小的
</pre><h3><a name='header-n369' class='md-header-anchor '></a>整合数据</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n370" mdtype="fences" style="page-break-inside: unset;"># t(数据框名称) 转置函数,将数据框行列互换
# aggregate()函数 整合函数,先将数据按行分组,然后对每组数据进行统计
aggregate(air_data$SEG_KM_SUM,by=list(air_data$FFP_TIER),mean)
# 先按会员等级进行分类,然后查看不同等级会员对应的平均里程数
# by的值可以有多个,逗号隔开
sample()函数
sample(1:100,10)	# 第一个值为范围（此处也可以直接写100）,第二个为抽取样本数量
sample(letters[1:20],10)	# 此函数也可以用于字符向量
sample(letters[1:20],10,replace=T)
# replace参数设定样本抽取是否有放回,默认为F,即不放回。不放回时,抽取样本数不能大于样本总数,放回时可以
set.seed()	# 在运行sample函数前运行,可将随机数固定
# 举例：
nrow1&lt;-sample(nrow(air_data),100)
newdata&lt;-air_data[nrow1,]
</pre><h2><a name='header-n371' class='md-header-anchor '></a>DAY13 第11讲 缺失值的识别与处理</h2><h3><a name='header-n372' class='md-header-anchor '></a>什么是缺失值</h3><p>当前数据集中数据的不完全,缺失值可能是未作答问题,也有可能因为设备故障或者编码数据出现问题等,根据缺失值出现原因,可以分为两类：一种是机械原因,另一种是人为原因。机械原因是指因机械的因素导致数据收集或保存失败导致数据的缺失。人问原因是指人的主观失误、意识局限或者有意隐瞒造成的数据缺失。</p><h3><a name='header-n374' class='md-header-anchor '></a>缺失值的识别‘</h3><p>R中缺失值符号为NA。</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n376" mdtype="fences">data()	# 查看R中自带的数据集
mydata&lt;-mtcars
mydata[-(1:5,1)]&lt;-NA
mydata&lt;-edit(mydata)	# 随机变动数据为缺失值
isnadata&lt;-is.na(mydata)	# 查看是否为缺失值
sum(isnadata[,1]/nrow(isnadata))	# 查看缺失比例 
men(isnadata[,1])	# 查看缺失比例,,和上一行等价
apply(isnadata,2,mean)	# 查看每一列的缺失情况
apply(isnadata,1,mean)	# 查看每一行的缺失情况
which(isnadata,arr.ind=T)	# 查找缺失值位置,第一列返回行的下标,第二行     返回列的下标
complete.cases(mydata)	# 返回值为逻辑向量 ,识别矩阵或数据集中没有缺失值的行,没有缺失值的行返回TRUE,有缺失值的行返回FALSE
mean(!complete.cases(mydata))	# 查看缺失比例,为有缺失值的行比上总行数
mydata[!complete.cases(mydata),]	# 查看存在缺失值的行
</pre><h3><a name='header-n377' class='md-header-anchor '></a>缺失模式探索</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n378" mdtype="fences">install.packages("mice")
library(mice)
md.pattern(mydata)
</pre><p>输出矩阵显示了缺失值的情况,数值为该行缺失值的个数。最后一列数值显示缺失值的模式,即一行观测存在几个缺失值。第一列表示相应缺失模式下观测的数量。表中间数值表示缺失值的模式,表示该列中是否有缺失值,0为该列有缺失值,1为没有缺失值。最后一行表示相应变量存在缺失值的数量。最右下角的数值为数据集中缺失值的总个数。</p><h3><a name='header-n380' class='md-header-anchor '></a>缺失值处理</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n381" mdtype="fences">mydata_1&lt;-mydata[,-1]	# 删除缺失值多的第一列
na.omit(mydata_1)	# 删除缺失值观测
mydata_1[complete.cases(mydata),]	# 保留没有缺失值的行
which(is.na(mydata),arr.ind=T)
mydata[is.na(mydata[,1],1)&lt;-mean(mydata[,1],na,rm=T)	# 将第一列缺失值赋值为第一列剔除缺失值后的平均值
</pre><h2><a name='header-n382' class='md-header-anchor '></a>DAY14 第12讲 异常值的识别与处理</h2><h3><a name='header-n383' class='md-header-anchor '></a>什么是异常值</h3><p>异常值是指样本中明显偏离其他观测值的个别数。</p><p>异常值的来源分为：人为错误（输入错误等）、测量误差（最常见来源,测量仪器使用错误等）、抽样错误、自然异常值。</p><p>异常值的影响：大幅度地改变数据分析和统计建模的结果,回归和分类建模最容易受到异常值的影响。</p><h3><a name='header-n387' class='md-header-anchor '></a>异常值的识别方法</h3><h4><a name='header-n388' class='md-header-anchor '></a>range()函数</h4><p>查看变量的取值范围,看是否超出了合理范围</p><h4><a name='header-n390' class='md-header-anchor '></a>3sigmar方法</h4><p>要求样本主体呈正态分布或近似正态分布。在原则下变量的取值比平均值范围超过三倍标准差的值就是异常值。</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n392" mdtype="fences" style="page-break-inside: unset;">rnorm(100)	# 生成100个正态分布的随机数
plot(density(rnorm(100)))	# 查看一下图形
d&lt;-c(rnorm(100),200)	# d中有一个200的异常值
m&lt;-mean(d)	# 找出平均值
s&lt;-sd(d)	# 找出标准差
u&lt;-m+3*s
l&lt;-m-3*s
d[d&lt;1|d&gt;u]	# 输出异常值
# 可以将以上几行代码使用code-extract function功能创建为函数。
sigma&lt;-function(d) {
m&lt;-mean(d)
s&lt;-sd(d)
u&lt;-m+3*s
l&lt;-m-3*s
d[d&lt;l|d&gt;u]
}
# 然后即可直接使用,如：
sigma(mydata[,1])
sapply(mydata,sigma)
</pre><h4><a name='header-n393' class='md-header-anchor '></a>箱线图分析</h4><p>箱线图又称盒虚图或盒式图,可以显示数据分散的情况,因为形状像箱子所以常叫做箱线图。它主要能反映原始变量分布的特征,而且可以多组数据分布特征的比较。箱线图中间最粗的线代表中位数；盒子的下面一条线为下四分位数,盒子的上面一条线为上四分位数,上下四分位数的差是四分位间距。最上面和最下面的线分别为上下边缘,是由上四分位数+1.5倍四分位间距和下四分位数-1.5倍四分位间距得来的。超出上下边缘的值即为异常值。</p><p>根据此原理自定义函数:</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n396" mdtype="fences">boxplot(mydata[,1])

boxout&lt;-function(x) {
prob75&lt;-quantile(x,probs=0.75)	# 上四分位数
prob25&lt;-quantile(x,probs=0.25)	# 下四分位数
iqr=prob75-prob25	# 四分位间距
u=prob75+1.5*iqr	# 上边缘
l=prob25-1.5*iqr	# 下边缘
x[x&lt;l|x&gt;u]	# 输出异常值
}
boxout(mydata[,1])
sapply(mydata,boxout)
</pre><p>用箱线图查找异常值的结果和用3sigma方法查找的结果是不一样的,按照需求选择合适的方法。</p><h4><a name='header-n398' class='md-header-anchor '></a>离群点</h4><p>离群点方法是将异常值观测视为离群点。</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n400" mdtype="fences">boxout&lt;-function(x) {
prob75&lt;-quantile(x,probs=0.75)	# 上四分位数
prob25&lt;-quantile(x,probs=0.25)	# 下四分位数
iqr=prob75-prob25	# 四分位间距
u=prob75+1.5iqr	# 上边缘
l=prob25-1.5iqr	# 下边缘
c(1:length(x))[x&lt;l|x&gt;u]	# 返回异常值的位置
}
boxout(mydata[,1])
sapply(mydata,boxout)
</pre><h3><a name='header-n401' class='md-header-anchor '></a>异常值处理</h3><p>直接删除异常值</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n403" mdtype="fences">mydata[-c(1,31),]
</pre><p>变量变换</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n405" mdtype="fences">sigma
sapply(mydata,sigma)
log(mydata$mpg)	# 将异常值转换为自然对数,消除异常值
sigma(log(mydata$mpg))	# 检查显示无异常值
</pre><h2><a name='header-n406' class='md-header-anchor '></a>DAY15  第13讲数据可视化（1）</h2><p>plot函数</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n408" mdtype="fences" style="page-break-inside: unset;">mtcars
plot(mtcars$disp,mtcars$wt)	# 以mtcars数据集中的两列数据做点图
plot(mtcars$disp,mtcars$wt,pch=16)	# pch参数可以修改点图中符号的形状
plot(mtcars$disp,mtcars$wt,cex=1.5)	# cex参数可以控制符号的大小,默认值为1
plot(mtcars$disp,mtcars$wt,col="blue")	# col参数可以控制符号的颜色
plot(mtcars$disp,mtcars$wt,type="l")	# type参数可以设置图像的类型,如线图、点线图等。有时绘制折线图会出现图像杂乱,此时可以先将数据按照一定顺序排序后再绘图
wtcars&lt;-mtcars[order(mtcars$disp),]
plot(mtcars$disp,mtcars$wt,type="1",lty=2)	# lty参数可以设置线图中线的类型
plot(mtcars$disp,mtcars$wt,type="1",lty=2,lwd=2)# lwd参数可以设置线图中线的粗细,默认值为1
plot(mtcars$disp,mtcars$wt,type="1",lty=2,lwd=2,main="disp vs wt",sub="fubiaoti",
xlab="disp",ylab="wt",xlim=c(200,300))
# main为主标题,sub为副标题
# xlab为x轴标签,ylab为y轴标签
# xlim为x轴数值范围,ylim为y轴数值范围
# abline()函数,可以在图像上添加线
abline(h=c(3,4),v=c(200,400))	# h设置直线与y轴的交点,v设置直线与x轴的交点
# lty,lwd等参数也可以用于此函数
abline(lm(mtcars$wt~mtcars$disp))	# 添加回归线,lm函数为回归函数
m0&lt;-subset(mtcars,vs==0)  
m1&lt;-subset(mtcars,vs==1)
plot(m0$disp~m0$wt, pch=17,col="blue")
points(m1$disp,m1$wt,pch=17,col="green")
legend("bottomright",title="class of vs",
legend=c("v-shaped","straight"),col=c("blue","green",pch=c(16,17)))
# 添加图例(图例所在位置,图例的标题,图例类型,图例颜色,图例的图形)
# 查找各参数的取值和参数取值的效果,可以？par查看
# par()函数
# par()既可以查看各参数值的默认值,也可以修改参数
</pre><h2><a name='header-n409' class='md-header-anchor '></a>DAY16 第14讲 数据可视化（2）</h2><h3><a name='header-n410' class='md-header-anchor '></a>条形图：常用于分类变量</h3><p>barplot()函数</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">matcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">gear</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">table</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">gear</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># table()函数用来计算频数</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">barplot</span>(<span class="cm-variable">table</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">gear</span>),<span class="cm-variable">names.arg</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-string">"gears-3"</span>,<span class="cm-string">"gears-4"</span>,<span class="cm-string">"gears-5"</span>),</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">ylim</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-number">0</span>,<span class="cm-number">20</span>),<span class="cm-variable">horiz</span><span class="cm-operator">=</span><span class="cm-variable">T</span>,<span class="cm-variable">col</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-string">"blue"</span>,<span class="cm-string">"green"</span>,<span class="cm-string">"red"</span>))</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># horiz参数为T/F,可以将条形图呈水平显示</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">table</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">gear</span>,<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">vs</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">barplot</span>(<span class="cm-variable">table</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">gear</span>,<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">vs</span>), <span class="cm-variable">col</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-string">"blue"</span>,<span class="cm-string">"green"</span>,<span class="cm-string">"red"</span>),<span class="cm-variable">xlab</span><span class="cm-operator">=</span><span class="cm-string">"vs"</span>) &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 堆积条形图,横坐标为vs,纵坐标为gear</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">legend</span>(<span class="cm-string">"topright"</span>,<span class="cm-variable">title</span><span class="cm-operator">=</span><span class="cm-string">"gear"</span>,<span class="cm-variable">legend</span><span class="cm-operator">=</span><span class="cm-string">"3:5"</span>,<span class="cm-variable">col</span><span class="cm-operator">=</span> <span class="cm-variable">c</span>(<span class="cm-string">"blue"</span>,<span class="cm-string">"green"</span>,<span class="cm-string">"red"</span>),<span class="cm-variable">pch</span><span class="cm-operator">=</span><span class="cm-number">15</span>,<span class="cm-variable">cex</span><span class="cm-operator">=</span><span class="cm-number">0.6</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">barplot</span>(<span class="cm-variable">table</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">gear</span>,<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">vs</span>), <span class="cm-variable">col</span><span class="cm-operator">=</span><span class="cm-variable">c</span>(<span class="cm-string">"blue"</span>,<span class="cm-string">"green"</span>,<span class="cm-string">"red"</span>),<span class="cm-variable">xlab</span><span class="cm-operator">=</span><span class="cm-string">"vs"</span>,<span class="cm-variable">beside</span><span class="cm-operator">=</span><span class="cm-variable">T</span>) &nbsp;</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># beside默认值为F,若设为T,则条形图不为堆叠形式,而是并列条形图</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 299px;"></div><div class="CodeMirror-gutters" style="display: none; height: 299px;"></div></div></div></pre><h3><a name='header-n413' class='md-header-anchor '></a>饼图：也可用于分类变量</h3><p>pie()函数</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">pie</span>(<span class="cm-variable">table</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">gear</span>),<span class="cm-variable">col</span><span class="cm-operator">=</span><span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">3</span>,<span class="cm-variable">lables</span><span class="cm-operator">=</span> <span class="cm-variable">paste</span>(<span class="cm-string">"gears-"</span>,<span class="cm-number">3</span><span class="cm-operator">:</span><span class="cm-number">5</span>,<span class="cm-string">":"</span>,<span class="cm-variable">table</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">gear</span>),<span class="cm-variable">sep</span><span class="cm-operator">=</span><span class="cm-string">""</span>))</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># col的值可以是字符型的也可以是整数型的</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># pie()函数也可以添加图例</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">a</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">rainbow</span>(<span class="cm-number">20</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 生产一个20种颜色的调色板</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">pie</span>(<span class="cm-variable">rep</span>(<span class="cm-number">1</span>,<span class="cm-number">20</span>),<span class="cm-variable">col</span><span class="cm-operator">=</span><span class="cm-variable">a</span>,<span class="cm-variable">lables</span><span class="cm-operator">=</span><span class="cm-variable">a</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 将调色板用饼图呈现出来</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 138px;"></div><div class="CodeMirror-gutters" style="display: none; height: 138px;"></div></div></div></pre><h3><a name='header-n416' class='md-header-anchor '></a>直方图：常用于连续型变量</h3><p>hist()函数</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">hist</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">disp</span>,<span class="cm-variable">freq</span><span class="cm-operator">=</span><span class="cm-variable">F</span>,<span class="cm-variable">breaks</span><span class="cm-operator">=</span><span class="cm-number">5</span>)</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 23px;"></div><div class="CodeMirror-gutters" style="display: none; height: 23px;"></div></div></div></pre><p>freq设置y轴频率,breaks控制分组</p><h3><a name='header-n420' class='md-header-anchor '></a>核密度图</h3><p>plot(density())函数</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">plot</span>(<span class="cm-variable">density</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">disp</span>))</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 23px;"></div><div class="CodeMirror-gutters" style="display: none; height: 23px;"></div></div></div></pre><p>如果要在直方图基础上添加核密度图,则使用 lines(density(mtcars$disp))</p><h2><a name='header-n424' class='md-header-anchor '></a>DAY17 第15讲  数据可视化（3）</h2><h3><a name='header-n425' class='md-header-anchor '></a>箱线图</h3><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">a</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">boxplot</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">wt</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 将值赋值给a,除生成箱线图外,还生成名称为 a含有6个元素的列表,其中out元素显示的是异常值</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">a</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">out</span><span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 查看异常值</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">boxplot.stats</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">wt</span>)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 无箱线图图像,输出结果为对象的各项数值</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 其中out为异常值,stats为最小值最大值和四分位数,注意：此处最小值和最大值不是数值中的最值,而是根据四分位数推算出的合理范围</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">boxplot</span>(<span class="cm-variable">wt</span><span class="cm-operator">~</span><span class="cm-variable">vs</span>,<span class="cm-variable">data</span><span class="cm-operator">=</span><span class="cm-variable">mtcars</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 输出两个箱线图,分别为vs=0和vs=1对应的wt的值</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">boxplot</span>(<span class="cm-variable">wt</span><span class="cm-operator">~</span><span class="cm-variable">vs</span><span class="cm-operator">*</span><span class="cm-variable">gear</span>,<span class="cm-variable">data</span><span class="cm-operator">=</span><span class="cm-variable">mtcars</span>,<span class="cm-variable">col</span><span class="cm-operator">=</span><span class="cm-number">2</span><span class="cm-operator">:</span><span class="cm-number">7</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 回归曲线</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">plot</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">disp</span>,<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">wt</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># lowess()函数绘制拟合曲线</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">lines</span>(<span class="cm-variable">lowess</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">wt</span><span class="cm-operator">~</span><span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">disp</span>))<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 因变量（y轴）~自变量（x轴）</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 276px;"></div><div class="CodeMirror-gutters" style="display: none; height: 276px;"></div></div></div></pre><p>数据越多,图点越多,拟合曲线越光滑</p><h3><a name='header-n428' class='md-header-anchor '></a>散点图矩阵</h3><p>观察多个变量之间的二元关系</p><p>pairs()函数绘制散点图矩阵</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">pairs</span>(<span class="cm-operator">~</span><span class="cm-variable">mpg</span><span class="cm-operator">+</span><span class="cm-variable">disp</span><span class="cm-operator">+</span><span class="cm-variable">wt</span>,<span class="cm-variable">data</span><span class="cm-operator">=</span><span class="cm-variable">mtcars</span>)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># formula参数为~mpg+disp+w</span></span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 23px;"></div><div class="CodeMirror-gutters" style="display: none; height: 23px;"></div></div></div></pre><p>对角线上为变量名称</p><ul><li>以第一行第二列散点图为例,其x轴为disp,y轴为mpg</li><li>以第一行第三列列散点图为例,其x轴为wt,y轴为mpg</li></ul><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n438" mdtype="fences">pairs(~mpg+disp+wt,data=mtcars,upper.panel=NULL)
</pre><p>右上角的散点图即被去掉</p><p>col,pch等参数也适用于pairs()函数</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">pairs</span>(<span class="cm-operator">~</span><span class="cm-variable">mpg</span><span class="cm-operator">+</span><span class="cm-variable">disp</span><span class="cm-operator">+</span><span class="cm-variable">wt</span>,<span class="cm-variable">data</span><span class="cm-operator">=</span><span class="cm-variable">mtcars</span>,<span class="cm-variable">upper.panel</span><span class="cm-operator">=</span><span class="cm-atom">NULL</span>,<span class="cm-variable">subset</span><span class="cm-operator">=</span><span class="cm-variable">vs</span><span class="cm-operator">==</span><span class="cm-number">1</span>)</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 23px;"></div><div class="CodeMirror-gutters" style="display: none; height: 23px;"></div></div></div></pre><p>仅取变量中vs==1的子集作图,subset参数可用于查看某一类变量的关系</p><h3><a name='header-n443' class='md-header-anchor '></a>气泡图</h3><p>可展示三个定量变量直接的关系,先创建一个二维的散点图,然后用点的大小来表示第三变量的值,多用于商业领域</p><p>symbols()函数</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">symbols</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">disp</span>,<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">wt</span>,<span class="cm-variable">circles</span><span class="cm-operator">=</span><span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span>,<span class="cm-variable">inches</span><span class="cm-operator">=</span><span class="cm-number">0.3</span>)</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 23px;"></div><div class="CodeMirror-gutters" style="display: none; height: 23px;"></div></div></div></pre><p>circles参数为第三个变量,由气泡半径代表其值的大小</p><p>如果想用气泡的面积来表示第三变量的大小,则将代码修改为：</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">symbols</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">disp</span>,<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">wt</span>,<span class="cm-variable">circles</span><span class="cm-operator">=</span><span class="cm-variable">sqrt</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span><span class="cm-operator">/</span><span class="cm-variable">pi</span>),<span class="cm-variable">inches</span><span class="cm-operator">=</span><span class="cm-number">0.3</span>)</span></pre></div></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 23px;"></div><div class="CodeMirror-gutters" style="display: none; height: 23px;"></div></div></div></pre><p>inches参数控制气泡大小</p><p>如果想用气泡颜色的深浅来表示第三变量的大小,则将代码修改为：</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">heat.colors</span>(<span class="cm-number">32</span>)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 产生32个颜色</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">barplot</span>(<span class="cm-variable">rep</span>(<span class="cm-number">1</span>,<span class="cm-number">32</span>),<span class="cm-variable">col</span><span class="cm-operator">=</span><span class="cm-variable">heat.colors</span>(<span class="cm-number">32</span>))<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 用条形图查看一下</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">mtcars</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">mtcars</span>(<span class="cm-variable">order</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span>,<span class="cm-variable">decreasing</span><span class="cm-operator">=</span><span class="cm-variable">T</span>),)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 先对变量重新排序</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">symbols</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">disp</span>,<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">wt</span>,<span class="cm-variable">circles</span><span class="cm-operator">=</span><span class="cm-variable">sqrt</span>(<span class="cm-variable">mtcars</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span><span class="cm-operator">/</span><span class="cm-variable">pi</span>),<span class="cm-variable">inches</span><span class="cm-operator">=</span><span class="cm-number">0.3</span>,<span class="cm-variable">bg</span><span class="cm-operator">=</span><span class="cm-variable">heat.colors</span>(<span class="cm-number">32</span>))</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 115px;"></div><div class="CodeMirror-gutters" style="display: none; height: 115px;"></div></div></div></pre><p>不仅以气泡面积表示第三变量的值,也以颜色深浅表示,颜色越深值越大</p><h3><a name='header-n454' class='md-header-anchor '></a>绘制函数图像</h3><p>curve()函数</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R" style="page-break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">curve</span>(<span class="cm-variable">x</span><span class="cm-operator">^</span><span class="cm-number">3</span><span class="cm-operator">-</span><span class="cm-number">3</span><span class="cm-operator">*</span><span class="cm-variable">x</span>,<span class="cm-operator">-</span><span class="cm-number">2</span>,<span class="cm-number">2</span>,<span class="cm-variable">n</span><span class="cm-operator">=</span><span class="cm-number">1000</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># curve(函数表达式,x的取值范围,绘制的点的数量) </span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># n越大,点越大,曲线越光滑</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">curve</span>(<span class="cm-variable">x</span><span class="cm-operator">^</span><span class="cm-number">2</span><span class="cm-operator">-</span><span class="cm-number">2</span>,<span class="cm-operator">-</span><span class="cm-number">2</span>,<span class="cm-number">2</span>,<span class="cm-variable">n</span><span class="cm-operator">=</span><span class="cm-number">1000</span>,<span class="cm-variable">col</span><span class="cm-operator">=</span><span class="cm-string">"blue"</span>,<span class="cm-variable">add</span><span class="cm-operator">=</span><span class="cm-variable">T</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># add参数为T时,新添曲线叠加在原曲线上</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 绘图配色方案</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">library</span>(<span class="cm-variable">rColorBrewer</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">display.brewer.a</span><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 生成3套配色方案</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 第一套为渐变色,多用于连续变量</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 第二套颜色彼此区分明显,多用于分类变量</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 第三套为两头深中间浅,便于标记数据中的离群点</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 最左边为配色方案名称,可以根据名称在代码中引用配色方案</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span cm-text="">​</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># brewer.pal()函数引用配色方案</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">brewer.pal</span>(<span class="cm-variable">n</span><span class="cm-operator">=</span><span class="cm-number">3</span>,<span class="cm-string">"Dark2"</span>)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># n为使用颜色的数量,"Dark2"为配色方案名称</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">cols</span><span class="cm-operator cm-arrow">&lt;-</span> <span class="cm-variable">brewer.pal</span>(<span class="cm-variable">n</span><span class="cm-operator">=</span><span class="cm-number">3</span>,<span class="cm-string">"Dark2"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">barplot</span>(<span class="cm-variable">rep</span>(<span class="cm-number">1</span>,<span class="cm-number">3</span>),<span class="cm-variable">col</span><span class="cm-operator">=</span><span class="cm-variable">cols</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 将配色运用到绘图中</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 345px;"></div><div class="CodeMirror-gutters" style="display: none; height: 345px;"></div></div></div></pre><h2><a name='header-n457' class='md-header-anchor '></a>DAY18 第16讲 描述性统计</h2><h3><a name='header-n458' class='md-header-anchor '></a>数值型变量</h3><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R" style="page-break-inside: unset;"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">catacol</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">c</span>(<span class="cm-string">"vs"</span>,<span class="cm-string">"am"</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">mydata</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">mtcars</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">mydata</span>[<span class="cm-variable">catacol</span>]<span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">lapply</span>(<span class="cm-variable">mydata</span>[<span class="cm-variable">catacol</span>],<span class="cm-variable">as.factor</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 将mtcars中vs和am两列变量的数据类型转换为因子型</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">numdata</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">mydata</span>[<span class="cm-variable">sapply</span>(<span class="cm-variable">mydata</span>,<span class="cm-variable">is.numeric</span>)]<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 取mydata中的数值型变量</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">catadata</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">mydata</span>[<span class="cm-variable">catacol</span>]<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 取mydata中的因子型变量</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 数值型变量的描述性统计</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 偏度：统计数据分布偏斜方向和程度的度量,是统计数据分布非对称程度的数字特征。正态分布时,偏度为0；右偏分布又叫正偏分布,偏度大于0；右偏分布是指与正态分布相比,右边极端值较多,也就是存在较多# 比均值大的极端值。左偏分布又叫负偏分布,偏度小于0；左偏分布是指与正态分布相比,左边极端值较多,也就是存在较多比均值小的极端值。</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">install.packages</span>(<span class="cm-string">"fBasics"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">library</span>(<span class="cm-variable">fBasics</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">skewness</span>(<span class="cm-variable">numdata</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># 查看mpg的偏度</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 峰度：又称峰度系数,表示概率密度分布曲线在平均值处峰值高低的特征数。直观来看,峰度反映了分布的尖度。一般正态分布的峰值为3,厚尾即峰度大于3,表示远离均值的数值比较多,分布比正态分布分散；瘦尾即峰度小于3,表示分布比正态分布集中。</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">kurtosis</span>(<span class="cm-variable">numdata</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span>,<span class="cm-variable">method</span><span class="cm-operator">=</span><span class="cm-string">"moment"</span>)<span class="cm-tab" role="presentation" cm-text="	">   </span><span class="cm-comment"># method参数可以选用不同的方法</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">plot</span>(<span class="cm-variable">density</span>(<span class="cm-variable">numdata</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span>))<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 生成图像观察一下</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-comment"># 正态性检验 </span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">Jarquebera</span>检验</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">jarqueberatest</span>(<span class="cm-variable">numdata</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span>)<span class="cm-tab" role="presentation" cm-text="	"> </span><span class="cm-comment"># 查看p值,如果p值小于0.1,则发生假设的概率小,有理由拒绝原假设,p值越小拒绝原假设的理由越充足；如果p值大于0.1,则结果不显著,没有理由拒绝原假设。</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">y</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">jarqueberatest</span>(<span class="cm-variable">numdata</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">y</span>@<span class="cm-variable">test</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">p.value</span><span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 查看p值</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">mysummary</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-keyword">function</span>(<span class="cm-variable">x</span>) {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">mean</span><span class="cm-operator">=</span><span class="cm-variable">mean</span>(<span class="cm-variable">x</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">sd</span><span class="cm-operator">=</span><span class="cm-variable">sd</span>(<span class="cm-variable">x</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">library</span>(<span class="cm-variable">fBasics</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">skew</span><span class="cm-operator">=</span><span class="cm-variable">skewness</span>(<span class="cm-variable">x</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">kur</span><span class="cm-operator">=</span><span class="cm-variable">kurtosis</span>(<span class="cm-variable">x</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">y</span><span class="cm-operator">=</span><span class="cm-variable">jarqueberaTest</span>(<span class="cm-variable">x</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">p</span><span class="cm-operator">=</span> <span class="cm-variable">y</span>@<span class="cm-variable">test</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">p.value</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-builtin">return</span>(<span class="cm-variable">c</span>(<span class="cm-variable">mean</span><span class="cm-operator">=</span><span class="cm-variable">mean</span>,<span class="cm-variable">sd</span><span class="cm-operator">=</span><span class="cm-variable">sd</span>,<span class="cm-variable">skew</span><span class="cm-operator">=</span><span class="cm-variable">skew</span>,<span class="cm-variable">kur</span><span class="cm-operator">=</span><span class="cm-variable">kur</span>,<span class="cm-variable">p</span>))</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">mysummary</span>(<span class="cm-variable">numdata</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">mpg</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 创建函数,输出mpg的各项参数</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">apply</span>(<span class="cm-variable">numdata</span>,<span class="cm-number">2</span>,<span class="cm-variable">mysummary</span>)<span class="cm-tab" role="presentation" cm-text="	">  </span><span class="cm-comment"># 输出一个矩阵,呈现各列的各项参数</span></span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 851px;"></div><div class="CodeMirror-gutters" style="display: none; height: 851px;"></div></div></div></pre><h3><a name='header-n460' class='md-header-anchor '></a>分类变量</h3><p>列联表：观测数据时按照两个或更多属性分类时列出的频数表,是由两个以上变量进行交叉分类的频数分布表。</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n462" mdtype="fences">table(catadata)	# 输出catadata这一分类数据的列联表,表中数值为频数
prop.table(table(catadata))	# 将上一行结果的频数转换为频率
prop.table(table(catadata),1)	# 输出结果为单个元素占行数据和的比例
prop.table(table(catadata),2)	# 输出结果为单个元素占列数据和的比例
margin.table(table(catadata))	# 生成编辑（全部）频数,输出结果为32即catadata数据的频数一共有32个
margin.table(table(catadata),1)	# 输出vs对应的属性及其频数
margin.table(table(catadata)),2)	# 输出am对应的属性及其频数
propx&lt;- prop.table(table(catadata))
addmargins(table(catadata))	# 添加编辑和；生成3行3列的数据集,多出一行一列,计算各行各列的频数和
addmargins(propx)	# 输出结果为频率的列联表
</pre><h2><a name='header-n463' class='md-header-anchor '></a>DAY20 第17讲 相关性分析</h2><h3><a name='header-n464' class='md-header-anchor '></a>图表分析查看相关性</h3><p>在R中用图表分析相关性是一种比较粗糙的分析,通过散点图的分布可以看出变量之间的联系。ggplot包是基于图形图层的语法,使图形的重复更新更简单,每次只要更新一个特征,ggplot包也可以从更高维度角度审视图层的构成,认为图形的每个组件都是可以被修改的。在ggplot2中一个图形被认为是包含了数据、映射、图层、标度、坐标和分面的列表,用ggplot函数和图层函数逐步进行作图。</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n466" mdtype="fences">library(ggplot2)
ggplot(mtcars,mapping=aes(x=disp,y=wt))	# 用ggplot()函数来作图,它可以创建一个坐标系,可以在其上添加图层,mapping参数指的是一种映射,常和aes()成对出现,aes()中是横纵坐标系
ggplot(mtcars,mapping=aes(x=disp,y=wt,colour=factor(vs)))+ geom_point()
# geom_point()添加点层,然后按vs变量属性得分类给点着色
ggplot(mtcars,mapping=aes(x=disp,y=wt,colour=factor(vs)))+ geom_point()+stat_smooth()
# stat_smooth()函数添加拟合曲线
ggplot(mtcars,mapping=aes(x=disp,y=wt,colour=factor(vs)))+ geom_point()+stat_smooth()+coord_cartesian(xlim=c(100:400))
# 添加笛卡尔坐标限制取值范围
</pre><h3><a name='header-n467' class='md-header-anchor '></a>相关系数计算</h3><p>相关系数可以定量描述变量之间的相关关系,相关系数的符号代表相关关系的方向,符号为正,表示变量之间正相关,符号为负,表示变量之间负相关。相关系数的大小表示关系的强弱程度,完全不相关时,相关系数为0,完全相关时,相关系数为1,相关系数的取值范围是-1~1。相关系数的绝对值越大,表示变量越相关。计算相关系数常用的有三种方法,有：Pearson相关系数,Spearman相关系数,Kendall相关系数。</p><p>Pearson相关系数：可以计算两个数值变量的Pearson相关系数,计算方法是变量之间的协方差除以各自标准差的乘积。但是使用Pearson相关系数对数据有比较高的要求,一般要求数据来自正态分布的总体,而且要求数据之间差距不能太大,因为Pearson相关系数对异常值比较敏感。</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n470" mdtype="fences">cor(mtcars[,-c(8,9)])	# cor()函数,填入数据集名称即可(去除非数值型变量的列)
# Spearman相关系数：又称为Spearman秩相关系数,秩可以理解为一种顺序或排序,它是按照原始数据的排序顺序来求解的。Spearman相关系数没有Pearson相关系数那么多现在条件。
cor(mtcars[,-c(8,9)],method="spearman")	# 指定相关系数的方法
# Kendall相关系数：也是用秩来求相关系数,不同之处在于可以用于计算分类变量求的相关系数。也可以用于计算数组变量相关系数。
cor(mtcars[,-c(8,9)],method="kendall")
</pre><p>三者均输出相关系数矩阵,部分数据因方法不同结果不同,可按照具体情况选用不同计算方法。</p><h3><a name='header-n472' class='md-header-anchor '></a>相关系数显著性检验</h3><p>相关系数有没有统计学意义,相关系数为真为假需要相关系数显著性检验来判断。</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n474" mdtype="fences">cor.test()函数	# 此函数只能检验一个相关系数是否显著
cor.test(mtcars$disp,mtcars$wt)	# 默认为对Pearson相关系数进行检验
</pre><p>df为自由度</p><p>alternative hypothesis为备择假设（此处为相关系数不等于0）,第四行为95%的置信区间,最后一行为相关系数的值。</p><p>此处p值非常非常小,可以认为拒绝原假设,可以选择备择假设。</p><p>可以检验多个相关系数的包：</p><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n479" mdtype="fences">install.packages(psych)
library(psych)
corr.test()函数	# 检验矩阵的相关系数,输出结果为相关系数矩阵和p值矩阵两部分。
# 相关系数矩阵对称,p值矩阵不对称,p值矩阵左下角未经调整,右上角为调整过的p值矩阵,我们观察右上角矩阵来判断相关系数是否显著。p值大于0.1,没有理由拒绝原假设,反之则有理由拒绝。
corr.test(mtcars[,-c(8,9)],method="spearman")	# 此函数也可以选择相关系数的计算方法
corr.test(mtcars[,-c(8,9)],method="spearman",adjust="none")	# adjust参数可以设置右上角矩阵也不经调整,则p值矩阵对称
</pre><h3><a name='header-n480' class='md-header-anchor '></a>相关系数矩阵可视化</h3><pre spellcheck="false" class="md-fences mock-cm md-end-block" lang="R" contenteditable="false" cid="n481" mdtype="fences">library(ggcorrplot)
# ggcorrplot()函数,将相关矩阵系数可视化。
a&lt;-cor(mtcars[,-c(8,9),method="spearman")
ggcorrplot(a)
# 右侧corr即表示相关系数大小
ggcorrplot(a,lab=T,lab_size=2.5)	# 可在色块上显示相关系数具体数值,lab_size可改字的大小
ggcorrplot(a,lab=T,lab_size=2.5,tl.cex=7)	# cex参数可以改变变量名称的字体大小
ggcorrplot(a,lab=T,lab_size=2.5,tl.cex=7,type="lower")	# 去重,只显示左上角（upper）或右下角
ggcorrplot(a,lab=T,lab_size=2.5,tl.cex=7,type="lower",colors=c("red","white","green"))	# 改变颜色
</pre><h2><a name='header-n482' class='md-header-anchor '></a>DAY21 第18讲 聚类分析的案例</h2><h3><a name='header-n483' class='md-header-anchor '></a>导入数据 </h3><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">library</span>(<span class="cm-variable">readxl</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">data18</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">read_excel</span>(<span class="cm-string">"data18.xlsx"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">view</span>(<span class="cm-variable">data18</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 69px;"></div><div class="CodeMirror-gutters" style="display: none; height: 69px;"></div></div></div></pre><h3><a name='header-n485' class='md-header-anchor '></a>缺失值、异常值处理</h3><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">isnadata</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">is.na</span>(<span class="cm-variable">data18</span>)</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">apply</span>(<span class="cm-variable">isnadata</span>,<span class="cm-number">2</span>,<span class="cm-variable">sum</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">apply</span>(<span class="cm-variable">isnadata</span>,<span class="cm-number">2</span>,<span class="cm-variable">sum</span>)[<span class="cm-variable">apply</span>(<span class="cm-variable">isnadata</span>,<span class="cm-number">2</span>,<span class="cm-variable">sum</span>)<span class="cm-operator">!=</span><span class="cm-number">0</span>]</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">data</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">data18</span>[<span class="cm-operator">!</span>(<span class="cm-variable">is.na</span>(<span class="cm-variable">data18</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">SUM_YR_1</span>)<span class="cm-operator">|</span><span class="cm-variable">is.na</span>(<span class="cm-variable">data18</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">SUM_YR_2</span>))]</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">summary</span>(<span class="cm-variable">data</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">SUM_YR</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">SUM_YR_1</span><span class="cm-operator">+</span><span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">SUM_YR_2</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">x</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">data</span>[<span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">SUM_YR</span><span class="cm-operator">==</span><span class="cm-number">0</span><span class="cm-operator">&amp;</span><span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">SEG_KM_SUM</span><span class="cm-operator">!=</span><span class="cm-number">0</span>,<span class="cm-variable">c</span>(<span class="cm-string">"SUM_YR"</span>,<span class="cm-string">"</span> <span class="cm-string">SEG_KM_SUM"</span>)]</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">data</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">data</span>[<span class="cm-operator">!</span>(<span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">SUM_YR</span><span class="cm-operator">==</span><span class="cm-number">0</span><span class="cm-operator">&amp;</span><span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">SEG_KM_SUM</span><span class="cm-operator">!=</span><span class="cm-number">0</span>),]</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 184px;"></div><div class="CodeMirror-gutters" style="display: none; height: 184px;"></div></div></div></pre><h3><a name='header-n487' class='md-header-anchor '></a>RMF模型</h3><p>经常用于客户管理中衡量客户价值,主要考虑三个指标：最近一次消费,消费频率和消费金额。</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation"><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">L</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">LOAD_TIME</span><span class="cm-operator">-</span><span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">FFP_DATE</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">L</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">as.numeric</span>(<span class="cm-variable">data</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">L</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">RMFdata</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">data</span>[<span class="cm-variable">c</span>(<span class="cm-string">"L"</span>,<span class="cm-string">"LAST_TO_END"</span>,<span class="cm-string">"FLIGHT_COUNT"</span>,<span class="cm-string">"SEG_KM_SUM"</span>,<span class="cm-string">"avg_discount"</span>)]</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 69px;"></div><div class="CodeMirror-gutters" style="display: none; height: 69px;"></div></div></div></pre><h3><a name='header-n490' class='md-header-anchor '></a>K-Means聚类</h3><p>聚类前首先要将数据进行归一化处理,进行归一化处理的目的是为了加快聚类的收敛速度,避免数据淹没以及降低奇异数据对算法的敏感度。</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">max_min</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-keyword">function</span>(<span class="cm-variable">x</span>) {</span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">min</span><span class="cm-operator">=</span><span class="cm-variable">min</span>(<span class="cm-variable">x</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">max</span><span class="cm-operator">=</span><span class="cm-variable">max</span>(<span class="cm-variable">x</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">(<span class="cm-variable">x</span><span class="cm-operator">-</span><span class="cm-variable">min</span>)<span class="cm-operator">/</span>(<span class="cm-variable">max</span><span class="cm-operator">-</span><span class="cm-variable">min</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">} &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  这个过程就叫做归一化</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">RMFdata</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">apply</span>(<span class="cm-variable">RMFdata</span>,<span class="cm-number">2</span>,<span class="cm-variable">max_min</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">summary</span>(<span class="cm-variable">RMFdata</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 161px;"></div><div class="CodeMirror-gutters" style="display: none; height: 161px;"></div></div></div></pre><p>经过变换后,所有值得最大最小值都在0~1之间,各数据不再具有量纲,方便进行不同数据之间的比较,变量的重要性是一样的</p><p>聚类是把物理或抽象对象的集合分成相似的对象类的过程。聚类的目的是把一个给定的数据对象集合进行若干个划分,同一个划分内的数据对象之间具有很强的相似性,彼此之间的相似性应该尽可能地大,不同的划分对象之间是有差异的,划分与划分之间的差异度越大越好,一个划分被称为一个簇（cluster）。聚类被广泛运用于模式识别、空间数据分析等。</p><p>K-Means聚类：从观测里选K个对象作为初始聚类中心,K是可以选择的,可以选择把数据分成几个类,然后计算每个数据与选定的初始的聚类中心的距离,把数据划分到距它最近的初始聚类中,然后计算每个聚类的均值,然后将均值当作新的聚类中心,不断重复以上过程,直到最后聚类结果不再变化为止。K-Means聚类适合较为集中的大样本数据。</p><pre spellcheck="false" class="md-fences md-end-block ty-contain-cm md-focus" lang="R"><div class="CodeMirror cm-s-inner CodeMirror-wrap" lang="r"><div style="overflow: hidden; position: relative; width: 3px; height: 0px; top: 0px; left: 4px;"><textarea autocorrect="off" autocapitalize="off" spellcheck="false" tabindex="0" style="position: absolute; bottom: -1em; padding: 0px; width: 1000px; height: 1em; outline: none;"></textarea></div><div class="CodeMirror-scrollbar-filler" cm-not-content="true"></div><div class="CodeMirror-gutter-filler" cm-not-content="true"></div><div class="CodeMirror-scroll" tabindex="-1"><div class="CodeMirror-sizer" style="margin-left: 0px; margin-bottom: 0px; border-right-width: 0px; padding-right: 0px; padding-bottom: 0px;"><div style="position: relative; top: 0px;"><div class="CodeMirror-lines" role="presentation"><div role="presentation" style="position: relative; outline: none;"><div class="CodeMirror-measure"><pre><span>xxxxxxxxxx</span></pre></div><div class="CodeMirror-measure"></div><div style="position: relative; z-index: 1;"></div><div class="CodeMirror-code" role="presentation" style=""><div class="CodeMirror-activeline" style="position: relative;"><div class="CodeMirror-activeline-background CodeMirror-linebackground"></div><div class="CodeMirror-gutter-background CodeMirror-activeline-gutter" style="left: 0px; width: 0px;"></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">y</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">kmeans</span>(<span class="cm-variable">RMFdata</span>,<span class="cm-number">5</span>)<span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-comment"># 聚成5类</span></span></pre></div><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">result</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">rep</span>(<span class="cm-number">1</span>,<span class="cm-number">10</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-keyword">for</span> (<span class="cm-variable">k</span> <span class="cm-keyword">in</span> <span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">10</span>) {</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">y</span><span class="cm-operator cm-arrow">&lt;-</span><span class="cm-variable">kmeans</span>(<span class="cm-variable">RMFdata</span>,<span class="cm-variable">centers</span><span class="cm-operator">=</span><span class="cm-variable">k</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-tab" role="presentation" cm-text="	">    </span><span class="cm-variable">y</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">betweenss</span><span class="cm-operator">/</span><span class="cm-variable">y</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">betweenss</span><span class="cm-operator">/</span><span class="cm-variable">y</span><span class="cm-operator cm-dollar">$</span><span class="cm-variable">totss</span></span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;">}</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">plot</span>(<span class="cm-number">1</span><span class="cm-operator">:</span><span class="cm-number">10</span>,<span class="cm-variable">result</span>,<span class="cm-variable">type</span><span class="cm-operator">=</span><span class="cm-string">"b"</span>)</span></pre><pre class=" CodeMirror-line " role="presentation"><span role="presentation" style="padding-right: 0.1px;"><span class="cm-variable">kmeans</span>(<span class="cm-variable">RMFdata</span>,<span class="cm-number">3</span>)</span></pre></div></div></div></div></div><div style="position: absolute; height: 0px; width: 1px; border-bottom-width: 0px; border-bottom-style: solid; border-bottom-color: transparent; top: 184px;"></div><div class="CodeMirror-gutters" style="display: none; height: 184px;"></div></div></div></pre></div>
</body>
</html>